Created
February 15, 2022 12:48
-
-
Save JadenGeller/0eca3d4bf74620b35fcded266acb6197 to your computer and use it in GitHub Desktop.
Deeply union a type
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
export type DeepUnion<T, V> = T extends object | |
? { [P in keyof T]: DeepUnion<T[P], V> } | |
: T | V; | |
// type Example1 = DeepUnion<["hello", [1, 2, 3]], null>; | |
// = ["hello" | null, [1 | null, 2 | null, 3 | null]]; | |
// type Example2: = DeepUnion<{ name: string, age: number }, undefined>; | |
// = DeepUnion<{ name: string | undefined, age: number | undefined }, undefined>; | |
// type Example3 = DeepUnion<{ [name: string]: number }, string>; | |
// = { [name: string]: number | string }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment