You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constnums: number[]=[1,2,3,4,5];// only numbersconstproducts: Array<string>=["bread","milk"];// only stringsconstsomeValues: [string,boolean]=["ts",true];// only 1 string and 1 boolean
any Allows to disable type checking, as a result the variable can dynamically change it's type. Avoid any in your code.
letvalue: 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.
// extending a typetypeCat=Animal&{color: string;};
// value with 2 typestypeID=string|number;constuserId: ID=239053;constitemId: ID="A934VL";
Interfaces An interface declaration is another way to name an object type. Interfaces support a more convenient extension and are used more often.
interfaceIPerson{firstName: string;lastName?: string;// an optional parametergender: "female"|"male";// only certain valuesisMarried: boolean;}//Extending an interfaceinterfaceIWorkerextendsIPerson{job: string;}constsomeone: IWorker={firstName: "Alex",gender: "male",isMarried: true,job: "engineer",};
Functions
Basic usage We can describe the types of function arguments as well as the return value
// type "void" means that our function does not return anythingfunctionlogInfo(name: string,age: number): void{console.log(`My name is ${name}, I'm ${age} y.o.`);}// Arrow functionconstsquare=(x: number): number=>x*x;
Default arguments When default parameters are specified, their types are automatically determined by the passed values
Modifiers Various modifiers allow you to define the scope of variables and methods
classExample{constructor(a: number,b: number){this.a=a;this.b=b;}/* prevents assignments to the field outside of the constructor */readonlya: number;/* visible only inside classes inherited from the current class (not from outside) */protectedb: number;/* visible only inside current class (not from inherited and outside) */privatesecret="hello";alert=()=>{};}
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.
Basic usage Generics allows to create a component that can work over a variety of types rather than a single one
functiontest<T>(value: T): T{returnvalue;}// "T" will be replaced by the type we specifiedtest<string>("Hello World");test<number>(2022);test<boolean>(true);
Type checking You can safely use type checking with the typeof operator to execute the appropriate logic
functionlogger<T>(value: T): void{if(typeofvalue=="string"){// we are pretty sure that only strings will get hereconsole.log(value.toLocaleUpperCase());// therefore, we can use string methods}elseif(typeofvalue=="number"){console.log(value.toFixed(2));}else{console.log(`${value} has the ${typeofvalue} type`);}}logger<string>("Hello World");// "HELLO WORLD"logger<number>(2022);// 2022.00logger<boolean>(true);// "true has the boolean type"
Multiple generics You can pass multiple generics, for example, to describe different input and output types in functions