TypeScript cheatsheet
// 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