Typescript Cheatsheet - gecko-8/devwiki GitHub Wiki
Casting
Cast Object as Type
Use the "as" operator. Example:
document.getElementById('name') as HTMLInputElement;
Methods
Flag Return as Non-Nullable
Add an ! after it. Example:
document.getElementById('name')!; // Must return an element or fail
Types
Array
Single Type
Example:
let test: string[];
Multi-type
Example:
test: string | number | boolean
Enum
Example:
enum Role {
ADMIN,
READ_ONLY,
AUTHOR
}
Literal (only specific values)
Example:
test: 'value1' | 'value2'
Specific Values
Example:
enum Role {
ADMIN = 5,
READ_ONLY,
AUTHOR = 'Author'
}
Tuple (array with a specific number of values of specific types)
Example:
let test: [number, string];
Type Aliases (Custom Types)
Example:
type MultiType = number | string;
function test(input1: MultiType) {...}