Skip to content

Instantly share code, notes, and snippets.

@msichterman
Created May 24, 2023 22:57
Show Gist options
  • Save msichterman/6139c441d35e3bd25f545c06e7424f7b to your computer and use it in GitHub Desktop.
Save msichterman/6139c441d35e3bd25f545c06e7424f7b to your computer and use it in GitHub Desktop.
use conditional type inference to "copy" a type T into a new type variable O and then an identity-like mapped type which iterates through the copied type's properties. The conditional type inference is conceptually a no-op, but it's used to distribute union types and to force the compiler to evaluate the "true" branch of the conditional (if you …
// https://stackoverflow.com/a/69288824
export type Expand<T> = T extends (...args: infer A) => infer R
? (...args: Expand<A>) => Expand<R>
: T extends infer O
? { [K in keyof O]: O[K] }
: never;
export type ExpandRecursively<T> = T extends (...args: infer A) => infer R
? (...args: ExpandRecursively<A>) => ExpandRecursively<R>
: T extends object
? T extends infer O
? { [K in keyof O]: ExpandRecursively<O[K]> }
: never
: T;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment