Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. TypeScript Dictionary
  3. Template Literal Types

Template Literal Types

Since: TypeScript 4.1(2020)

This feature brings JavaScript's template literal (backtick string) syntax to the type level. It lets you express string patterns as types, and when combined with union types, automatically generates all possible string combinations.

Syntax

type TypeName = `prefix${Type}suffix`;

// Combined with union types, generates all string combinations.
type TypeName = `${UnionType1}-${UnionType2}`;

Syntax Overview

SyntaxDescription
`${Type}`The basic form of a template literal type. Represents a string pattern that includes the string representation of the type.
`${Union} pattern`Each member of the union type is expanded into the template, generating a union of all combined strings.
Uppercase<S>A built-in utility type that converts a string type to uppercase.
Lowercase<S>A built-in utility type that converts a string type to lowercase.
Capitalize<S>A built-in utility type that converts the first character of a string type to uppercase.
Uncapitalize<S>A built-in utility type that converts the first character of a string type to lowercase.

Sample Code

type Greeting = `Hello, ${string}!`;
let msg: Greeting = "Hello, TypeScript!"; // Works correctly.
// let msg2: Greeting = "Hi, TypeScript!"; // Error: does not match the pattern.

// Combined with union types, all combinations are generated automatically.
type Color = "red" | "green" | "blue";
type Size = "sm" | "md" | "lg";

// Generates "red-sm" | "red-md" | "red-lg" | "green-sm" | ... (9 combinations total).
type ColoredSize = `${Color}-${Size}`;
let cs: ColoredSize = "red-md"; // Works correctly.
// let cs2: ColoredSize = "red-xl"; // Error: "xl" is not in Size.

// Typing event name patterns.
type EventName = "click" | "focus" | "blur";
type HandlerName = `on${Capitalize<EventName>}`; // "onClick" | "onFocus" | "onBlur"

let handler: HandlerName = "onClick"; // Works correctly.

// Constraining object key patterns with template literal types.
type CSSProperty = "margin" | "padding";
type CSSDirection = "Top" | "Right" | "Bottom" | "Left";
type CSSShorthand = `${CSSProperty}${CSSDirection}`;
// Results in "marginTop" | "marginRight" | ... | "paddingLeft" (8 combinations total).

let prop: CSSShorthand = "marginTop"; // Works correctly.

// String transformation with built-in utility types
type Upper = Uppercase<"hello">; // "HELLO"
type Lower = Lowercase<"WORLD">; // "world"
type Cap = Capitalize<"typescript">; // "Typescript"

const upper: Upper = "HELLO";
console.log(upper); // Outputs: "HELLO"

Overview

Template literal types were introduced in TypeScript 4.1. They use backticks and placeholders (`${Type}`) to express string patterns at the type level — applying the same syntax as JavaScript template literals to type definitions.

The most powerful use case is combining them with union types. When you embed two union types in a template literal type, TypeScript automatically generates a string union representing the Cartesian product (every possible combination). This lets you define types for regularly patterned strings — such as CSS property names (marginTop, paddingLeft) or event handler names (onClick, onFocus) — without manually listing each one.

Additionally, four built-in utility types — Uppercase, Lowercase, Capitalize, and Uncapitalize — let you transform the casing of string literal types at the type level.

If you find any errors or copyright issues, please .