Skip to content

Instantly share code, notes, and snippets.

@dagda1
Last active February 9, 2022 06:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagda1/fd661892dbc3486e096f471c58375a71 to your computer and use it in GitHub Desktop.
Save dagda1/fd661892dbc3486e096f471c58375a71 to your computer and use it in GitHub Desktop.
// both examples below start
type UnionToXXX<T> = (T extends any ? (t: T) => T : never) extends infer U ? // rest
// Converts { x: string } | { y: number } to { x: string, y: number }
type UnionToIntersection<T, U = T> = (U extends any ? (arg: U) => any : never) extends ((arg: infer I) => void) ? I : never;
// Converts string | number | boolean to [string, number, boolean]
type UnionToTuple<T> = (
(
(
T extends any
? (t: T) => T
: never
) extends infer U
? (U extends any
? (u: U) => any
: never
) extends (v: infer V) => any
? V
: never
: never
) extends (_: any) => infer W
? [...UnionToTuple<Exclude<T, W>>, W]
: []
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment