Basic Template Literal Syntax
What you'll learn: How to embed type placeholders inside backtick strings to create dynamic string literal types.
The Core Idea
Just like JavaScript's template literals let you embed values into strings using ${expression}, TypeScript's template literal types let you embed types into string patterns using ${Type}. This creates new string literal types based on type combinations.
Instead of writing runtime strings, you're creating compile-time type patterns. The result is a string literal type that TypeScript understands and can autocomplete.
The Syntax
The pattern looks like this:
type Greeting = `Hello, ${string}`;
You use:
- Backticks (
`) to define the template ${Type}placeholders to insert types into the string pattern- Any combination of literal strings and type placeholders
Simple Examples
type Greeting = `Hello, ${string}`;
// Accepts any string starting with "Hello, "
type Status = `${number}%`;
// Accepts any string that's a number followed by %
type Command = `get-${string}`;
// Accepts "get-user", "get-product", etc.
The placeholder can be a union type too:
type Color = "red" | "blue" | "green";
type ColorName = `${Color}-color`;
// Result: "red-color" | "blue-color" | "green-color"
TypeScript distributes over the union, creating every possible combination.
Why This Matters
This lets you build type-safe patterns for CSS classes, API routes, event names, or any string convention your code follows — all with full autocomplete and type checking.
Key Takeaway: Template literal types use ${Type} placeholders inside backticks to combine types into string patterns, creating precise, autocomplete-friendly string literal types.