Last active
October 11, 2019 10:41
-
-
Save sobstel/3be5d2e62b306cdefb93d3b91c00ae34 to your computer and use it in GitHub Desktop.
TypeScript cheatsheet
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
// same arg type and return type | |
withUID<T>(obj: T) | |
// "extends" helps providing some constraints | |
// eg <T extends object> | |
withUID({a: 1}) // valid | |
withUID("wrong way") // NOT valid | |
// default value type (string) | |
A<T=string> { name: T } | |
// defining a type arg on top of another | |
MyFunc<T extends Person, S = T & { ssid: string }>( | |
person: S | |
) : S {} | |
// overloads (have no body) | |
function getArray(...args: string[]): string[]; | |
function getArray(args: number): undefined[]; | |
function getArray(...args) { | |
} | |
// component | |
class MyComponent extends Component<Props, State> { | |
state: State = {} | |
} | |
// partial subtype | |
type Partial<T> = { [P in keyof T]?: T[P]; } | |
type LocalUser = Partial<User>; | |
// exclude | |
Exclude<T, U> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment