Skip to article content
FrontendXLesson 23 / 40

Recursive and deep types

Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.

advanced · about 50 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Write a recursive conditional type with a base case.
  • Bound recursion with a depth parameter.
  • Recognize when recursion is too expensive.

Overview

#

Recursion needs a base case

snippet.ts
type DeepReadonly<T> = T extends (infer U)[]
  ? readonly DeepReadonly<U>[]
  : T extends object
    ? { readonly [K in keyof T]: DeepReadonly<T[K]> }
    : T;

The T extends object branch stops on primitives; without it, the type would recurse forever.

Distributing over unions

Conditional types distribute over naked type parameters, which is often what you want for a union of shapes.

Depth counters

To limit work, carry a depth parameter and decrement it each step.

snippet.ts
type Limited<T, D extends 0 | 1 | 2 = 2> = D extends 0
  ? T
  : T extends object
    ? { [K in keyof T]: Limited<T[K], D extends 2 ? 1 : 0> }
    : T;

Practical examples

DeepPartial, a Paths<T> type for dotted keys, and Flatten<T> for nested arrays all use recursion.

Cost

Each recursion step is type instantiation. Deeply nested or self-referential types can hit the instantiation limit and slow the editor. Keep depth small and add a base case first.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Implement `DeepReadonly<T>` that makes every nested property readonly and leaves primitives unchanged.

    starter.typescript
    export type DeepReadonly<T> = T extends object
      ? { readonly [K in keyof T]: T[K] }
      : T;
    

    Reveal the reference answer
    reference.typescript
    export type DeepReadonly<T> = T extends object
      ? { readonly [K in keyof T]: DeepReadonly<T[K]> }
      : T;
    

    Authored expected output: Compile-time: DeepReadonly<{ a: { b: number } }> is { readonly a: { readonly b: number } }. No runtime output.

    The conditional stops on primitives and the mapped type recurses into each property.

  2. Exercise 2Predict

    What is `R`? ```ts type Flatten<T> = T extends (infer U)[] ? Flatten<U> : T; type R = Flatten<number[][][]>; ```

    Choose the best answer
  3. Exercise 3Debug

    This type hits the instantiation limit. Add a base case so it stops on primitives. ```ts type Deep<T> = { [K in keyof T]: Deep<T[K]> }; ```

    starter.typescript
    type Deep<T> = { [K in keyof T]: Deep<T[K]> };
    

    Reveal the reference answer
    reference.typescript
    type Deep<T> = T extends object
      ? { [K in keyof T]: Deep<T[K]> }
      : T;
    

    Authored expected output: Compile error resolved: adding a T extends object guard gives the recursion a base case.

    Without the guard the type recurses into primitives forever.

Common misconceptions

#
  • Recursive types need a runtime loop.
  • A recursive type always terminates.
  • Deeper recursion is always more accurate.

Follow-up practice

#
  • typescript-template-parsing

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.