Last active
June 10, 2023 16:18
-
-
Save mlhaufe/2062be6c005337e03b23d55d1812a3bb to your computer and use it in GitHub Desktop.
TypeScript Union and Intersection types
This file contains 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
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never | |
type IntersectionToUnion<I> = (I extends any ? (x: I) => any : never) extends ((x: infer U) => any) ? U : never; | |
/** | |
* Zip two tuples together into a tuple of tuples | |
* @example | |
* ZipTuple<['name', 'age', 'isActive'], [string, number, boolean]> | |
* => [["name", string], ["age", number], ["isActive", boolean]] | |
*/ | |
export type ZipTuple<T extends readonly any[], U extends readonly any[]> = { | |
[K in keyof T]: [T[K], K extends keyof U ? U[K] : never] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment