Template Literal Type
In one line
A template literal type uses template literal syntax at the type level to create string types that match a specific pattern.
In simple words
Template literal types bring template literal syntax to the type level. type Greeting = Hello ${string}`` matches any string starting with "Hello ". This lets you constrain strings to specific patterns.
They are useful for defining event names, CSS property values, URL patterns, and other structured strings. Combined with union types and mapped types, they can create very precise string types.
For example, type HttpMethod = $${"GET" | "POST" | "PUT"} /api/${string}`` matches strings like "GET /api/users" or "POST /api/posts".
Example
type EventName = `on${Capitalize<string>}`;
type T = EventName; // matches "onClick", "onLoad", etc.
const handler: EventName = "onClick"; // OKThe template literal type constrains the string to start with 'on' followed by a capitalized word.
Related terms
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.