-
-
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 …
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
// 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