Last active
February 9, 2022 06:16
-
-
Save dagda1/fd661892dbc3486e096f471c58375a71 to your computer and use it in GitHub Desktop.
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
// 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