TypeScript 7 reference · Type Operators and Keywords

TypeScript template literal types Operator

Build string literal types with template syntax: `on${string}` or combinations of unions like `${Size}-${Color}`.

TypeScript 7 Since 4.1

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

`prefix${Type}suffix`

Examples

Every combination of two unionsTry it yourself
type Size = "S" | "M";
type Color = "red" | "blue";
type Sku = `${Size}-${Color}`; // 4 combinations
const all: Sku[] = ["S-red", "S-blue", "M-red", "M-blue"];
console.log(all.length);

Output

4
Pattern-checked stringsTry it yourself
type CssLength = `${number}px` | `${number}rem`;
const a: CssLength = "16px";
// @ts-expect-error: must end in px or rem
const b: CssLength = "16pt";
console.log(a);

Output

16px

Version history

VersionChange
TypeScript 4.1Added

Was this page helpful?