Skip to content

Instantly share code, notes, and snippets.

@sobstel
Last active October 11, 2019 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sobstel/3be5d2e62b306cdefb93d3b91c00ae34 to your computer and use it in GitHub Desktop.
Save sobstel/3be5d2e62b306cdefb93d3b91c00ae34 to your computer and use it in GitHub Desktop.
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