0Pricing
TypeScript Academy · Lesson

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>>>; // string

Recursive 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

All lessons in this course

  1. Understanding infer in Conditional Types
  2. Building ReturnType and Parameters from Scratch
  3. Deeply Nested Inference Patterns
  4. Practical infer Use Cases: Unwrapping Promises
← Back to TypeScript Academy