Skip to content

Instantly share code, notes, and snippets.

@khalidx
Last active January 15, 2022 08:38
Show Gist options
  • Save khalidx/f9f9972203086b09b6d4d70949d61e10 to your computer and use it in GitHub Desktop.
Save khalidx/f9f9972203086b09b6d4d70949d61e10 to your computer and use it in GitHub Desktop.
Convert a TypeScript Union of Types to an Intersection of Types.

TypeScript: Union to Intersection

Convert a TypeScript Union of Types (something | somethingElse) to an Intersection of Types (something & somethingElse).

// See "MergO" from https://github.com/microsoft/TypeScript/issues/26058#issuecomment-553212998
export type UnionToIntersection<U extends object> =
  (U extends object ? (k: U) => void : never) extends (k: infer T) => void
    ? (T extends object ? T : object)
    : object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment