Created
January 9, 2020 11:59
-
-
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
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 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