thecodex.expert · The Codex Family of Knowledge
TypeScript

Template Literal Types

Introduced in TypeScript 4.1, template literal types let you build a type the same way you’d build a string — by interpolating other types directly into a literal pattern.

TypeScript 6.0 Since TypeScript 4.1 Last verified:
Canonical Definition

A template literal type looks exactly like a JavaScript template string but operates on types: interpolating a union type inside the backticks produces a new union covering every combination. Introduced in TypeScript 4.1 (2020), this is the mechanism behind the key-remapping as clause in mapped types (covered earlier) and lets a type precisely describe string patterns like event names, CSS properties, or API route paths.

Interpolating a union produces every combination

The result is a new union covering every combination of the interpolated union's members — not a single string, but every valid EventName the pattern can produce.

TypeScriptbasic_template.ts
type EventName = "click" | "scroll" | "resize";
type HandlerName = `on${Capitalize<EventName>}`;
// "onClick" | "onScroll" | "onResize"

function addHandler(name: HandlerName) { /* ... */ }
addHandler("onClick");    // fine
// addHandler("onclick");  // COMPILE ERROR: lowercase doesn't match the pattern

Combining with mapped types: real, practical use

This is exactly the pattern used earlier for the Getters<T> example — the two features (mapped types and template literal types) compose directly.

TypeScriptcss_properties.ts
type CSSProperty = "color" | "background" | "border";
type CSSVariable = `--${CSSProperty}`;
// "--color" | "--background" | "--border"

const styles: Record<CSSVariable, string> = {
    "--color": "blue",
    "--background": "white",
    "--border": "1px solid black",
};

Built-in intrinsic string manipulation types

TypeScript ships four built-in type-level string transformations — Uppercase, Lowercase, Capitalize, Uncapitalize — that operate purely on string literal types at compile time, with no runtime equivalent needed since they're erased entirely.

TypeScriptintrinsic_types.ts
type Loud = Uppercase<"hello">;        // "HELLO"
type Quiet = Lowercase<"HELLO">;        // "hello"
type Title = Capitalize<"hello">;       // "Hello"
type Mini = Uncapitalize<"Hello">;      // "hello"

Sources

1
Microsoft. TypeScript Handbook, "Template Literal Types", typescriptlang.org/docs/handbook/2/template-literal-types.html.