Created
January 24, 2024 07:58
-
-
Save tmatz/fb61de9c34f87c1f8bddfecfaa2675ae to your computer and use it in GitHub Desktop.
Make union a type that allows destructuring assignment
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
/** | |
* Make union a type that allows destructuring assignment | |
* @example | |
* ``` | |
* const props: { a: number } | { b: string } = ... | |
* const { a, b } = props as UnionDestructurable<typeof props> | |
* ``` | |
*/ | |
type UnionDestructurable< | |
Union, | |
UncommonKeys extends string = Exclude< | |
Union extends infer T ? keyof T : never, | |
keyof Union | |
> | |
> = Union & | |
(Union extends infer T | |
? { [K in UncommonKeys]?: K extends keyof T ? T[K] : never } | |
: never); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment