Course contentsShow
← TypeScript
Lesson 1742 of 1,79639. TypeScript PerformanceFree lesson

Avoid Recursive Types Without Base Cases

Ensure recursive type definitions have clear termination conditions to prevent infinite expansion.

Avoid Recursive Types Without Base Cases

You'll learn: Why recursive type definitions must have clear termination conditions to prevent infinite expansion that crashes the compiler.

The Problem: Infinite Type Expansion

Just like a recursive function needs a base case to stop calling itself, a recursive type definition needs a condition that ends the recursion. Without it, TypeScript tries to expand the type infinitely and eventually hits its recursion depth limit.

Think of it like looking into two mirrors facing each other—you see an endless reflection that goes on forever. The compiler does the same thing with recursive types that have no stopping point, and eventually it gives up with an error like:

Type instantiation is excessively deep and possibly infinite.

What Is a Base Case in Recursive Types?

A base case is a condition where the type stops referring to itself. It's the "escape hatch" that breaks the recursion cycle.

Here's what happens without a base case:

// ❌ BAD: No base case - infinite recursion
type InfiniteList<T> = {
  value: T;
  next: InfiniteList<T>; // Always recurses, never stops
};

This type says "there's always another InfiniteList," with no way to end the chain. TypeScript will explode trying to figure out what this looks like.

Here's the same concept with a proper base case:

// ✅ GOOD: Has a base case - can terminate
type LinkedList<T> = {
  value: T;
  next: LinkedList<T> | null; // null is the base case
};

The | null gives TypeScript an escape route. The recursion can stop when next is null.

Why This Matters for Performance

When you create a recursive type without a base case, the TypeScript compiler tries to instantiate it, expanding it deeper and deeper until it hits the maximum recursion depth. This process:

  • Slows down your builds dramatically
  • Can cause the compiler to crash
  • Makes IDE responsiveness suffer
  • Produces confusing error messages

Even if your runtime data structure would eventually terminate, the type system doesn't know that—it needs an explicit union with a non-recursive option.

Real-World Example: Tree Structures

// ❌ Without base case
type BadTree<T> = {
  value: T;
  children: BadTree<T>[]; // Array is always of BadTree
};

// ✅ With base case
type GoodTree<T> = {
  value: T;
  children: GoodTree<T>[] | []; // Empty array is the base case
};

// Even better - using optional
type BetterTree<T> = {
  value: T;
  children?: BetterTree<T>[]; // undefined terminates
};

Key Takeaway: Every recursive type must include a non-recursive alternative (union with null, undefined, empty array, or primitive) that allows TypeScript to stop expanding the type definition. This termination condition is your base case, and without it, you'll hit compiler recursion limits.