Tutorial by Examples

A boolean represents the most basic datatype in TypeScript, with the purpose of assigning true/false values. // set with initial value (either true or false) let isTrue: boolean = true; // defaults to 'undefined', when not explicitely set let unsetBool: boolean; // can ...
Like JavaScript, numbers are floating point values. let pi: number = 3.14; // base 10 decimal by default let hexadecimal: number = 0xFF; // 255 in decimal ECMAScript 2015 allows binary and octal. let binary: number = 0b10; // 2 in decimal let octal: number = 0o755; // 493 in de...
Textual data type: let singleQuotes: string = 'single'; let doubleQuotes: string = "double"; let templateString: string = `I am ${ singleQuotes }`; // I am single
An array of values: let threePigs: number[] = [1, 2, 3]; let genericStringArray: Array<string> = ['first', '2nd', '3rd'];
A type to name a set of numeric values: Number values default to 0: enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; let bestDay: Day = Day.Saturday; Set a default starting number: enum TenPlus { Ten = 10, Eleven, Twelve } or assign values: enum MyOddSet { Thre...

Any

When unsure of a type, any is available: let anything: any = 'I am a string'; anything = 5; // but now I am the number 5
If you have no type at all, commonly used for functions that do not return anything: function log(): void { console.log('I return nothing'); } void types Can only be assigned null or undefined.
Array type with known and possibly different types: let day: [number, string]; day = [0, 'Monday']; // valid day = ['zero', 'Monday']; // invalid: 'zero' is not numeric console.log(day[0]); // 0 console.log(day[1]); // Monday day[2] = 'Saturday'; // valid: [0, 'Saturday'] day[3] = fals...
When you create a function in TypeScript you can specify the data type of the function's arguments and the data type for the return value Example: function sum(x: number, y: number): number { return x + y; } Here the syntax x: number, y: number means that the function can accept two argum...
Example: function hello(name: string): string { return `Hello ${name}!`; } Here the syntax name: string means that the function can accept one name argument and this argument can only be string and (...): string { means that the return value can only be a string Usage: hello('StackOverfl...
String literal types allow you to specify the exact value a string can have. let myFavoritePet: "dog"; myFavoritePet = "dog"; Any other string will give a error. // Error: Type '"rock"' is not assignable to type '"dog"'. // myFavoritePet = "rock&qu...
A Intersection Type combines the member of two or more types. interface Knife { cut(); } interface BottleOpener{ openBottle(); } interface Screwdriver{ turnScrew(); } type SwissArmyKnife = Knife & BottleOpener & Screwdriver; function use(tool: SwissArmyKnife){ ...
A const Enum is the same as a normal Enum. Except that no Object is generated at compile time. Instead, the literal values are substituted where the const Enum is used. // Typescript: A const Enum can be defined like a normal Enum (with start value, specifig values, etc.) const enum NinjaActivity ...

Page 1 of 1