Skip to content

Instantly share code, notes, and snippets.

@jrson83
Forked from msichterman/expand.ts
Created February 13, 2024 02:43
Show Gist options
  • Save jrson83/ee7aa180bd461f62467b44201858f397 to your computer and use it in GitHub Desktop.
Save jrson83/ee7aa180bd461f62467b44201858f397 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