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
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
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
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
// 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
type Student = { | |
id: string; | |
age: number; | |
}; | |
type Employee = { | |
companyId: string; | |
}; | |
let person: Student & Employee; |
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: [string, string, number]; | |
list = ['apple', 'banana', 8.75]; // ✅ | |
list = ['apple', true, 8.75]; // ❌ - the second argument should be of type string | |
list = ['apple', 'banana', 10.33, 3]; // ❌ - the tuple specifies a length of 3, not 4 |
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
// Optional function parameter | |
function callMom(message?: string) { | |
if (!message) { | |
console.log('Hi mom. Love you. Bye.'); | |
} else { | |
console.log(message); | |
} | |
} | |
// Interface describing an object containing an optional property |
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
enum ThemeColors { | |
Primary = 'primary', | |
Secondary = 'secondary', | |
Dark = 'dark', | |
DarkSecondary = 'darkSecondary', | |
}; |
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 = <ArrayElementType>(len: number, elem: ArrayElementType) => { | |
return new Array<ArrayElementType>(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 |