Recursive Type Definitions
Write types that refer to themselves safely.
Recursive Type Definitions is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Recursive Type?
A recursive type is a type that refers to itself in its own definition. This lets you describe data structures of unbounded depth.
type LinkedList<T> = {
value: T;
next: LinkedList<T> | null;
};
// next is the same type again, ending at null.A Linked List Type
The classic example: each node holds a value and a next pointer to another node, or null to end the chain.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
const list: LinkedList<number> = {
value: 1,
next: { value: 2, next: null }
};
console.log(list.value, list.next?.value);The Base Case Matters
Every recursive type needs a terminator, here null, so the recursion can end. Without it, the type would be infinitely deep.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
const single: LinkedList<string> = { value: "only", next: null };
console.log(single.next);Building Longer Chains
You can nest as deeply as you like; the type allows any length because next is itself a list or null.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
const chain: LinkedList<number> = {
value: 10,
next: { value: 20, next: { value: 30, next: null } }
};
console.log(chain.next?.next?.value);Traversing a Linked List
A function can walk the chain by following next until it reaches null. The recursive type makes this fully type-safe.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
function toArray<T>(list: LinkedList<T> | null): T[] {
const out: T[] = [];
let cur = list;
while (cur) { out.push(cur.value); cur = cur.next; }
return out;
}
const l: LinkedList<number> = { value: 1, next: { value: 2, next: null } };
console.log(toArray(l));Recursive Functions Over Recursive Types
Recursive types pair naturally with recursive functions. Here we compute the length by recursing on next.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
function length<T>(list: LinkedList<T> | null): number {
return list === null ? 0 : 1 + length(list.next);
}
const l: LinkedList<string> = { value: "a", next: { value: "b", next: null } };
console.log(length(l));Prepending to a List
Because the type is uniform, building new lists is easy: wrap an existing list as the next of a new head node.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
function prepend<T>(value: T, list: LinkedList<T> | null): LinkedList<T> {
return { value, next: list };
}
const l = prepend(1, prepend(2, null));
console.log(l.value, l.next?.value);Optional vs Nullable Terminators
You can terminate with null or make next optional. Both end the recursion; choose based on how you want to represent the end.
type ListA<T> = { value: T; next: ListA<T> | null };
type ListB<T> = { value: T; next?: ListB<T> };
const a: ListA<number> = { value: 1, next: null };
const b: ListB<number> = { value: 1 };
console.log(a.value, b.value);Recursive Types Are Lazy
TypeScript only expands a recursive type as far as you actually use it. The definition can be self-referential without causing infinite work.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
// Using two levels only expands two levels.
const l: LinkedList<number> = { value: 1, next: { value: 2, next: null } };
console.log(l.next?.value);Mapping Over a List
A recursive map transforms each value while preserving the structure, returning a new list of the same shape.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
function mapList<T, U>(list: LinkedList<T> | null, fn: (v: T) => U): LinkedList<U> | null {
if (list === null) return null;
return { value: fn(list.value), next: mapList(list.next, fn) };
}
const l: LinkedList<number> = { value: 1, next: { value: 2, next: null } };
console.log(mapList(l, x => x * 10)?.value);Why Recursive Types Are Powerful
Self-referential types let you model arbitrarily nested data, lists, trees, and JSON, with full type safety and no fixed depth limit in your design.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
const l: LinkedList<boolean> = { value: true, next: { value: false, next: null } };
console.log(l.value, l.next?.value);Quick Check: Recursive Types
Test your understanding of recursive type definitions.
Recap: Recursive Type Definitions
You learned that a recursive type references itself, needs a base case like null to terminate, and pairs naturally with recursive functions to traverse, map, and build linked lists.
type LinkedList<T> = { value: T; next: LinkedList<T> | null };
const l: LinkedList<number> = { value: 1, next: null };
console.log(l.value);Frequently asked questions
Is the “Recursive Type Definitions” lesson free?
Yes — the full text of “Recursive Type Definitions” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Recursive Type Definitions”?
Write types that refer to themselves safely. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Recursive Type Definitions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Recursive Type Definitions
- Typing Tree Structures
- JSON Value Types
- Recursion Depth and Limits