Course contentsShow
← TypeScript
Lesson 1455 of 1,79633. Inference Inversion PatternsFree lesson

Deep Property Extraction with Nested infer

Chain multiple infer clauses to extract deeply nested type information from complex structures.

Deep Property Extraction with Nested infer

What you'll learn: How to chain multiple infer clauses together to extract type information from deeply nested structures like multi-level objects, complex function types, or nested generic containers.


When extracting type information from complex, nested structures, a single infer often isn't enough. You need to nest multiple infer clauses, each one peeling back another layer until you reach the deeply buried type you're after.

The Concept

Think of nested infer like opening a series of nested boxes. Each infer opens one box and captures what's inside, then you use another infer to open the next box within that. This pattern is essential when working with types like Promise<Array<T>>, object properties several levels deep, or function chains.

Simple Nested Example

Extracting the element type from a Promise of an Array:

type DeepArrayElement<T> = 
  T extends Promise<infer U>
    ? U extends Array<infer V>
      ? V
      : never
    : never;

type Result = DeepArrayElement<Promise<string[]>>;
// Result is string

Here, the first infer U captures string[], then the second infer V extracts string from the array.

Chaining Multiple Layers

You can go even deeper:

type ExtractDeepValue<T> = 
  T extends { data: infer D }
    ? D extends { result: infer R }
      ? R extends { value: infer V }
        ? V
        : never
      : never
    : never;

type Example = ExtractDeepValue<{
  data: { result: { value: number } }
}>;
// Example is number

Each infer peels back one property layer, chaining conditionals to drill down.

Practical Pattern: Nested Generics

Extracting from deeply wrapped generic types:

type UnwrapDeep<T> = 
  T extends Promise<infer U>
    ? UnwrapDeep<U>
    : T extends Array<infer V>
      ? UnwrapDeep<V>
      : T;

type Unwrapped = UnwrapDeep<Promise<Array<Promise<string>>>>;
// Unwrapped is string

This recursively unwraps both Promises and Arrays until hitting the base type.


Key Takeaway: Chain multiple infer clauses in nested conditional types to extract type information from complex, multi-layered structures—each infer acts as a stepping stone deeper into the type's structure.