TypeScript basics
Basic types
boolean\
const isLoading: boolean = true;string\
const message: string = "Hello World!";number\
const year: number = 2022;
const exp: number = 2.718;array There are some ways to declare an array
const nums: number[] = [1, 2, 3, 4, 5]; // only numbers
const products: Array<string> = ["bread", "milk"]; // only strings
const someValues: [string, boolean] = ["ts", true]; // only 1 string and 1 booleanany Allows to disable type checking, as a result the variable can dynamically change it's type. Avoid
anyin your code.
let value: any = "this is string";
value = 107;
value = false;
value = [1, 2, "a", "b", true];Custom types and interfaces
Type Aliases Good for describing types in simple objects or for variables that may have more than one type.
Interfaces An interface declaration is another way to name an object type. Interfaces support a more convenient extension and are used more often.
Functions
Basic usage We can describe the types of function arguments as well as the return value
Default arguments When default parameters are specified, their types are automatically determined by the passed values
Optional arguments\
Unlimited arguments You can describe an indefinite number of arguments of the same type using spread operator
Function type Use function type to describe methods
Classes
Modifiers Various modifiers allow you to define the scope of variables and methods
Extending a class Classes can easily be extended by adding new variables/methods. This also supports overriding methods, but in a way that ensures backward compatibility.
Abstract classes Abstract classes/methods do not allow you to create instances, but only serve for expansion
Interface implementation Interfaces can be used to describe classes
Enums
Numeric enums Enums allow to define a set of named constants. Numeric enums automatically assign numbers to named constants.
String & mixed enums\
Generics
Basic usage Generics allows to create a component that can work over a variety of types rather than a single one
Type checking You can safely use type checking with the
typeofoperator to execute the appropriate logic
Multiple generics You can pass multiple generics, for example, to describe different input and output types in functions
Extending generic type\
Last updated