Typing Recursive Data
A recursive type mentions its own name in its definition. It is how you type trees, linked lists, nested comments, and JSON. The recursion must go through an object or array member.
Detailed Explanation
A value that contains a smaller value of the same kind needs a type that can expand forever. TypeScript allows that on type aliases and interfaces, with a rule: the self-reference cannot be the entire right-hand side.
Legal:
type Json =
| string
| number
| boolean
| null
| Json[]
| { [key: string]: Json };
Illegal: type T = T | string in a way the compiler cannot unfold, or type T = T. The compiler needs a "box" — an array, an object property, a Promise, a function — so it can stop expanding when it compares types.
Interfaces recurse naturally: interface Node { value: number; next: Node | null }. Aliases do too, once the box is there. Prefer a discriminated union when the recursive structure has more than one kind of node (leaf vs branch). Exhaustiveness then works on the kind field, and the recursive member lives only on the branch.
Generic recursive types are how you type a tree of T: type Tree<T> = { value: T; children: Tree<T>[] }. The recursion is on the structure; T is the payload and should not itself be required to be a Tree unless you mean that.
Recursive conditional types transform recursive data: unwrap nested promises, flatten nested arrays, walk a path string. They instantiate until a base case or until the compiler's depth limit. Name intermediate results. Add a depth parameter (D extends number[] = []) if you need to cut off expansion.
Mutual recursion (type A = { b: B }; type B = { a: A }) is allowed and is how you type graphs of two node kinds. Keep the boxes.
Recursive types are erased. A Json annotation on JSON.parse does not validate nesting. The type describes what you intend to walk. The walk is a recursive function (or an explicit stack) whose parameter is that type.
When a recursive type becomes slow to check, it is usually a union that exploded (every path through a large object graph). Narrow with discriminants, or split the type so the compiler is not comparing a 40-member union to itself on every property.
Code Examples
1. JSON as a recursive alias.
// 1. JSON as a recursive alias.
type Json =
| string
| number
| boolean
| null
| Json[]
| { [key: string]: Json };
const payload: Json = { ok: true, retries: [1, 2, 3] };2. A discriminated tree.
// 2. A discriminated tree.
type Tree<T> =
| { kind: "leaf"; value: T }
| { kind: "branch"; children: Tree<T>[] };
function count<T>(tree: Tree<T>): number {
switch (tree.kind) {
case "leaf":
return 1;
case "branch":
return tree.children.reduce((n, child) => n + count(child), 0);
}
}3. Linked list via an interface.
// 3. Linked list via an interface.
interface List<T> {
value: T;
next: List<T> | null;
}
const list: List<number> = { value: 1, next: { value: 2, next: null } };Common Gotchas
The self-reference must be boxed
`type T = T[]` is fine; a bare `type T = T` is not. If the compiler says the type is circular, add an object or array layer.
Recursive conditionals hit "excessively deep"
add a base case that does not recurse, or cap depth. Do not expand a JSON type with a mapped walk over every key unless you must.
Interfaces and aliases both recurse
pick the keyword for merging vs unions, not for recursion.
Annotating `JSON.parse` as `Json` is still unchecked
validate at runtime if the source is untrusted.