General Flashcards

(8 cards)

1
Q

Command “tsc”

A

Compile ts files to js

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to initialize TypeSctipt configuration?

A

tsc –init

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Difference between types and interfaces?

A

Interfaces only for objects
Types for all

Interfaces can extend - Dog extends Animal
Types can extend - Test = Dog & Animal

Interfaces can be can be declared with same name several times and they will merge
Types can’t be declared several times with same name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Difference between enum and const enum

A

After compilation:
enum - will be IIFE:

enum Example {
    One = 1,
    Two = 2,
}
console.log(Example.One); // 1
console.log(Example[1]);    //  "One".

!!! Compiled to:

var Example;
(function (Example) {
    Example[Example["One"] = 1] = "One";
    Example[Example["Two"] = 2] = "Two";
})(Example || (Example = {}));
console.log(Example.One);   // 1
console.log(Example[1]);    // "One"

const enum - will be inlined, will be missing from the code:

const enum Example {
    One = 1,
    Two = 2,
}
console.log(Example.One);   // 1.

!!! Compiled to:
console.log(1);

Bundle size and perfomance:
enum: bigger bundle size, worse perfomance
const enum: less bundle size, better perfomance

Two-way access:
enum - possible to access by key and value
const enum - only value

Debug, loop:
enum - can be iterated in loop, debuged
const enum - impossible, object is missing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When it is better to use types and when interfaces?

A

Interfaces are generally preferred over type aliases when defining object shapes, especially when you need to take advantage of declaration merging, which type aliases do not support.

Interfaces are generally preferred for defining object shapes due to their ability to extend and merge, providing a clear and organized structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Function overload

A

This is an opportunity to define several variants of one function signature with different parameter types to provide more precise typing and behavior of the function depending on the passed arguments.

Overload Signatures:

type MakeDate = {
(timestamp: number): Date;
(m: number, d: number, y: number): Date;
};

Implementation Signature:

const makeDate:MakeDate = function (mOrTimestamp: number, d?: number, y?: number): Date {
if (d !== undefined && y !== undefined) {
return new Date(y, mOrTimestamp, d);
} else {
return new Date(mOrTimestamp);
}
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Generics

A

It is a mechanism that allows you to create generic types and functions that can work with different data types, ensuring type safety and code reuse.

type PaymentInfo<T> = { // T is a generic parameter
id: string;
amount: number;
currency: T; // "configure" the currency field type
}</T>

const paymentInfo: PaymentInfo<string> = // …</string>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Benefits of TypeScript

A

1) Static typing — errors are detected at the development stage
2) Improved Developer Experience — autocompletion and hints in the IDE
3) Self-documenting code — types explain the structure of data
4) Safety during refactoring — warnings about places of use
5) Simplified work with APIs and complex data structures
6) Integration with editors and linters — flexible customization of code quality
7) Compatibility with JavaScript — can be implemented gradually
8) Easier to scale projects and work in a team
9) Support for generics for universal and safe code
10 Excellent integration with popular frameworks and tools

How well did you know this?
1
Not at all
2
3
4
5
Perfectly