Combining String Literal Types
What you'll learn: How TypeScript template literals automatically create every possible combination when you use union types.
The Multiplication Effect
When you use a union type inside a template literal type, TypeScript doesn't just pick one option—it creates all possible combinations. Think of it like a multiplication table: if you have 2 choices for the first part and 3 choices for the second part, you get 6 total combinations.
This is called distribution, and it happens automatically.
How It Works
When you write a template literal type with unions, TypeScript distributes over each member of the union, creating a new union with every combination:
type Color = "red" | "blue";
type Size = "small" | "large";
type Product = `${Color}-${Size}`;
// Result: "red-small" | "red-large" | "blue-small" | "blue-large"
TypeScript multiplied 2 colors × 2 sizes = 4 combinations.
Multiple Union Points
You can have unions at multiple positions in the same template:
type Prefix = "get" | "set";
type Property = "Name" | "Age";
type Method = `${Prefix}${Property}`;
// Result: "getName" | "getAge" | "setName" | "setAge"
Each union position multiplies the total number of combinations.
Why This Matters
This automatic distribution makes template literal types incredibly powerful for modeling patterns. Instead of manually listing every variation, you describe the pattern once and let TypeScript generate all valid combinations—perfect for API routes, CSS class names, or method names.
Key Takeaway: Template literal types automatically distribute over union types, multiplying them together to create all possible string combinations without manual enumeration.