Skip to content

Instantly share code, notes, and snippets.

@Krisztiaan
Last active February 12, 2020 14:56
Show Gist options
  • Save Krisztiaan/c5b8bb4910dd0b2d91aac4653965268b to your computer and use it in GitHub Desktop.
Save Krisztiaan/c5b8bb4910dd0b2d91aac4653965268b to your computer and use it in GitHub Desktop.
Extra Typescript Types
type ValuesOf<T> = T extends { [key: string]: infer T } ? T : never
// type Parameters<T> = T extends (...args: infer T) => any ? T : never
type FirstParameter<T> = T extends (first: infer T, ...args: any) => any ? T : never
type SecondParameter<T> = T extends (first: any, second: infer T, ...args: any) => any ? T : never
type PromiseValue<T> = T extends Promise<infer U>
? U
: T extends (...args: any[]) => Promise<infer U>
? U
: T
type Require<T, P extends keyof T> = T & Required<Pick<T, P>>
type NonNullable<T> = Diff<T, null | undefined>
type SetStateReturn<T> = Readonly<[T, React.Dispatch<React.SetStateAction<T>>]>
type ContextValue<T> = T extends Context<infer U> ? U : undefined
type PartiallyRequired<T, K extends keyof T> = Partial<Omit<T, K>> & Required<Pick<T, K>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment