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

String Template Literal Type Explosion

How combining multiple template literal types creates exponentially large union types.

String Template Literal Type Explosion

You'll learn: How combining multiple template literal types creates exponentially large union types that can severely degrade TypeScript's performance.

The Multiplication Problem

When you use template literal types with union inputs, TypeScript generates every possible combination. Each union in the template multiplies with others:

type Color = "red" | "blue" | "green";
type Size = "small" | "medium" | "large";

// This creates 3 × 3 = 9 types:
type ColorSize = `${Color}-${Size}`;
// "red-small" | "red-medium" | "red-large" | 
// "blue-small" | "blue-medium" | "blue-large" |
// "green-small" | "green-medium" | "green-large"

This works fine with small unions. But watch what happens as you add more:

type Method = "get" | "post" | "put" | "delete";
type Resource = "user" | "post" | "comment" | "tag";
type Format = "json" | "xml" | "csv";

// This creates 4 × 4 × 3 = 48 types
type Endpoint = `${Method}-${Resource}-${Format}`;

When Explosion Becomes a Problem

The issue escalates rapidly. Adding just one more 5-member union takes you from 48 to 240 types. With five unions of 4 members each, you hit 1,024 types. The compiler must generate, store, and check all of them.

Think of it like a restaurant menu: if you have 3 proteins, 4 sides, and 3 sauces, that's manageable. But if you add 5 cooking styles and 6 toppings, suddenly you're advertising 360 different meals—and customers (the type checker) have to mentally process all of them.

Real-World Impact

Large template literal unions cause:

  • Slow compilation (seconds become minutes)
  • Memory exhaustion (TypeScript crashes)
  • Unreadable error messages (hundreds of string literals)
  • Poor IDE performance (lag when hovering)

Key Takeaway: Template literal types with multiple unions create a combinatorial explosion—use them sparingly and prefer smaller, focused unions or runtime validation for large pattern spaces.