This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The `name` parameter can be either a string or null | |
| const sayHappyBirthdayOnFacebook = (name: string | null) => { | |
| if (name === null) { | |
| console.log('Happy birthday!'); | |
| } else { | |
| console.log(`Happy birthday ${name}!`); | |
| } | |
| }; | |
| sayHappyBirthdayOnFacebook(null); // => "Happy birthday!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fillArray = <T>(len: number, elem: T) => { | |
| return new Array<T>(len).fill(elem); | |
| }; | |
| const newArray = fillArray<string>(3, 'hi'); // => ['hi', 'hi', 'hi'] | |
| newArray.push('bye'); // ✅ | |
| newArray.push(true); // ❌ - only strings can be added to the array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let dog: { | |
| kind: string; | |
| weight: number; | |
| }; | |
| dog = { | |
| kind: 'mammal', | |
| weight: 10, | |
| }; // ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Animal = { | |
| kind: string; | |
| weight: number; | |
| } | |
| let dog: Animal; | |
| dog = { | |
| kind: 'mammal', | |
| weight: 10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Animal { | |
| kind: string; | |
| weight: number; | |
| } | |
| let dog: Animal; | |
| dog = { | |
| kind: 'mammal', | |
| weight: 10, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // First method is using the square bracket notation | |
| let messageArray: string[] = ['hello', 'my name is fred', 'bye']; | |
| // Second method uses the Array keyword notation | |
| let messageArray: Array<string> = ['hello', 'my name is fred', 'bye']; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let aBoolean: boolean = true; | |
| let aNumber: number = 10; | |
| let aString: string = 'woohoo'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let list = [10, 22, 4, null, 5]; | |
| list.push(6); // ✅ | |
| list.push(null); // ✅ | |
| list.push('nope'); // ❌ - type 'string' is neither of type 'number' or 'null' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function add(a: number, b: number) { | |
| return a + b; | |
| } | |
| const result = add(2, 4); | |
| result.toFixed(2); // ✅ | |
| result.length; // ❌ - length is not a property of number types |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Default function parameters | |
| const tweetLength = (message = 'A default tweet') => { | |
| return message.length; | |
| } |