# Recursive Types

> Types that call themselves.

Subject: TypeScript | Lesson 34 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/recursive-types
Previous: [Intrinsic String Types](https://codewithanimation.in/typescript/intrinsic-string-types.md) | Next: [Mutability Limits](https://codewithanimation.in/typescript/mutability-limits.md)

## Infinite Nesting

Just like a function can call itself in JavaScript, a type can reference itself in TypeScript.

Recursive types are essential for modeling data structures that can be infinitely nested, like JSON objects, file directories, or linked lists.

## The DeepPartial Problem

The built-in `Partial<T>` utility only makes the *top-level* properties of an object optional. If you have nested objects, their properties remain required.

By creating a `DeepPartial<T>` recursive type, you can traverse every nested level of an object and apply the `?` modifier all the way down.

## Syntax

```typescript
// Base case: If T is an object, map over it and call DeepPartial on its children.
// Otherwise, just return T.
type DeepPartial<T> = T extends object ? {
  [K in keyof T]?: DeepPartial<T[K]>;
} : T;

type User = {
  id: number;
  settings: { theme: string; notifications: boolean };
};

type A = DeepPartial<User>;
// Both 'settings' AND 'theme'/'notifications' are now optional!
```

## Common mistake: Excessively Deep Instantiation

If you write a recursive type without a proper base case, or if you try to parse a data structure that is too complex, TypeScript will throw a famous error:

`Type instantiation is excessively deep and possibly infinite.` TypeScript enforces a recursion limit (usually around 50 levels) to prevent the compiler from crashing.

## Tip: Typing JSON

Recursive types are the only way to accurately type a JSON object: `type JSONValue = string | number | boolean | null | { [x: string]: JSONValue } | Array<JSONValue>;`

## Try it: Try it

Watch the `DeepPartial` type recursively traverse a nested configuration object, applying the optional `?` modifier to every level.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/recursive-types

## Check yourself

### Question 1

Why does a recursive type like `DeepPartial<T>` need a conditional check (`T extends object`)?

- A. To make sure it doesn't throw a runtime error.
- B. To act as a base case to terminate the recursion when it reaches primitive values (like string or number).
- C. Because all mapped types require a conditional check.

Correct answer: B

Without checking if the current type is an object, the recursive type would try to map over primitive values like strings or numbers indefinitely, crashing the compiler.

- Why not A: Types don't exist at runtime.
- Why not C: Mapped types don't require conditionals, but recursive ones need base cases.

## Remember this

- Recursive types reference themselves in their own definition.
- They must have a base case (usually a conditional check) to terminate.
- They are required for modeling infinite structures like JSON, Trees, or `DeepPartial`.
- TypeScript has strict limits on recursion depth to protect the compiler.
