Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
Last active June 10, 2023 16:18
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 mlhaufe/2062be6c005337e03b23d55d1812a3bb to your computer and use it in GitHub Desktop.
Save mlhaufe/2062be6c005337e03b23d55d1812a3bb to your computer and use it in GitHub Desktop.
TypeScript Union and Intersection types
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