Deeply Nested Inference Patterns
Apply infer recursively for complex type extraction.
Why Deep Inference?
Sometimes the type you want is buried inside several layers of generics. Deep inference uses recursive or chained conditional types to reach it.
type DeepUnwrap<T> =
T extends Promise<infer U>
? DeepUnwrap<U>
: T;
type A = DeepUnwrap<Promise<Promise<string>>>; // stringRecursive Conditional Types
TypeScript 4.1+ supports recursive conditional types, letting you peel away wrapper layers one at a time.
type Flatten<T> = T extends Array<infer U> ? Flatten<U> : T;
type B = Flatten<string[][][]>; // string