Command “tsc”
Compile ts files to js
How to initialize TypeSctipt configuration?
tsc –init
Difference between types and interfaces?
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
Difference between enum and const enum
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
When it is better to use types and when interfaces?
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.
Function overload
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);
}
};Generics
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>
Benefits of TypeScript
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