# Template Literal Types

> String interpolation at the type level.

Subject: TypeScript | Lesson 32 of 54 | 5 min read
Interactive lesson: https://codewithanimation.in/typescript/template-literal-types
Previous: [Key Remapping (as)](https://codewithanimation.in/typescript/key-remapping.md) | Next: [Intrinsic String Types](https://codewithanimation.in/typescript/intrinsic-string-types.md)

## Types that build Strings

Template Literal Types build on string literal types, allowing you to interpolate other types into a string via backticks.

Just like JavaScript string interpolation (`Hello ${name}`), but entirely within the type system!

## Combinatorial Explosion

If you are building a CSS or UI library, you might need hundreds of specific string combinations, like `margin-top-sm` or `border-red-500`.

Instead of writing them all out manually, you can provide unions to a template literal type, and TypeScript will automatically generate every possible combination.

## Syntax

```typescript
type Color = "red" | "blue";
type Size = "sm" | "lg";

// TypeScript generates all 4 combinations automatically!
type ClassName = `text-${Color}-${Size}`;
// "text-red-sm" | "text-red-lg" | "text-blue-sm" | "text-blue-lg"

// You can also use intrinsic string manipulation types
type EventName<T extends string> = `on${Capitalize<T>}`;
type ClickEvent = EventName<"click">; // "onClick"
```

## Common mistake: The Cross-Product Trap

Because template literal types generate the cross-product of all interpolated unions, they can grow exponentially.

If you interpolate three unions of 10 items each, you instantly generate 1,000 types. This can drastically slow down the TypeScript compiler or hit internal type complexity limits.

## Tip: Parsing Strings with infer

You can actually use the `infer` keyword *inside* a template literal type to parse strings!

`type ExtractPath<T> = T extends `/api/${infer Path}` ? Path : never;` will extract "users" from "/api/users".

## Try it: Try it

Type an event string in the input and watch the interpolation logic dynamically generate the properly cased handler name (e.g. `on${Capitalize<Event>}`).

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/template-literal-types

## Check yourself

### Question 1

Given `type A = 'x' | 'y'` and `type B = '1' | '2'`, what is the result of ``type C = `${A}${B}` ``?

- A. 'x1' | 'y2'
- B. 'x' | 'y' | '1' | '2'
- C. 'x1' | 'x2' | 'y1' | 'y2'

Correct answer: C

Template literal types automatically generate the Cartesian product (all combinations) of any union types interpolated into them.

- Why not A: Template literal types create the full cross-product of combinations.
- Why not B: It interpolates them together into single strings.

## Remember this

- Template literal types use backticks and `${}` interpolation.
- Interpolating union types automatically generates every possible combination.
- They are incredibly powerful for typing dynamic string paths, CSS classes, and event names.
- Be careful of combinatorial explosion slowing down your compiler.
