Skip to content

Instantly share code, notes, and snippets.

@fluidsonic
Created January 9, 2020 11:59
Show Gist options
  • Save fluidsonic/2ce8bfec735da0a1eab01d6d9bdfc277 to your computer and use it in GitHub Desktop.
Save fluidsonic/2ce8bfec735da0a1eab01d6d9bdfc277 to your computer and use it in GitHub Desktop.
Merges a union type into a type with properties of all union components
type Merge<Union> = AddOptionalProperties<Union, keyof UnionToIntersection<Union>>
type AddOptionalProperties<Union, Properties extends keyof any> =
Union extends (infer Component)
? { [P in keyof Union]: Union[P] } & { [P in Exclude<Properties, keyof Union>]?: never }
: never
// https://stackoverflow.com/a/50375286/1183577
type UnionToIntersection<Union> =
(Union extends any ? (_: Union) => void : never) extends ((_: infer Component) => void) ? Component : never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment