Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
Created February 15, 2022 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JadenGeller/0eca3d4bf74620b35fcded266acb6197 to your computer and use it in GitHub Desktop.
Save JadenGeller/0eca3d4bf74620b35fcded266acb6197 to your computer and use it in GitHub Desktop.
Deeply union a type
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