Build Times Exploding
You'll learn how certain type patterns can cause TypeScript's compiler to slow to a crawl during migration, and how to identify and fix them.
The Problem: Types That Make the Compiler Work Too Hard
During migration, you might suddenly notice tsc taking minutes instead of seconds. This happens when TypeScript's type checker encounters patterns that force it to explore an exponential number of type combinations.
Think of it like asking someone to match every person in one room with every person in another room, then with every person in a third room, and so on. The combinations explode quickly.
Common Culprits
Deep Type Recursion
Recursive conditional types that don't terminate quickly force the compiler to evaluate many nested layers. If you've built a DeepReadonly or DeepPartial type and applied it to large objects, each property level multiplies the work.
Wide Union Distributions
When you have a union with dozens of members and a conditional type distributes over it, TypeScript checks each member separately. Twenty union members become twenty separate type evaluations—and nested conditionals multiply that further.
Intersection Type Conflicts
Complex intersections with many properties force the compiler to check compatibility between every property pair. Deep object intersections are especially expensive.
Overuse of infer in Nested Conditionals
Each infer site creates a new type variable that TypeScript must solve for. Nested infer in recursive types compounds the problem.
Diagnosis and Fixes
Use tsc --extendedDiagnostics to see where time is spent. Look for files with unusually high check times.
Solutions:
- Simplify deep recursive types by limiting recursion depth
- Replace wide unions with discriminated unions when possible
- Break complex intersections into named intermediate types
- Use concrete types instead of generic inference where acceptable
- Consider runtime validation instead of pure compile-time guarantees
Key Takeaway: Type-level complexity has real compile-time costs. During migration, favor simple, explicit types over clever generic gymnastics. Ship working code first, optimize types later.