Template literal types

`on-${T}` types

A template literal type builds string types from a pattern. Placeholders like ${T} are filled in when the type is used.

EventName of click is the single string on-click. Any typo in the input produces a wrong event name that the compiler catches.

type EventName<T extends string> = `on-${T}`; const click: EventName<"click"> = "on-click"; console.log(click);

on-click

Typed event keys

Combine templates with keyof to stamp out one listener name per property. Each key of Props gets its own change event.

TitleChange is exactly the string change:title. Rename a property and every listener name follows it.

type Props = { title: string; open: boolean; }; type Listener<K extends keyof Props> = `change:${K}`; const titleChange: Listener<"title"> = "change:title"; console.log(titleChange);

change:title

Split strings

Templates also run backwards with infer to split strings apart. Match the pattern, capture the head and the rest, and return them as a tuple.

P is the pair of a and b-c for the input a-b-c. When nothing matches, the fallback keeps the whole string.

type Split<S extends string> = S extends `${infer H}-${infer Rest}` ? [H, Rest] : [S]; const p: Split<"a-b-c"> = ["a", "b-c"]; console.log(p[0], p[1]);

a b-c

Intrinsic Uppercase

TypeScript ships intrinsic helpers that rewrite string casing. Uppercase turns hey into HEY without any runtime code.

Lowercase, Capitalize, and Uncapitalize round out the set. They shine for normalizing keys and event names.

type Shout<T extends string> = Uppercase<T>; const loud: Shout<"hey"> = "HEY"; console.log(loud);

HEY

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a template literal type?

    Answer

    A string type built from a pattern with placeholders

    The backtick pattern describes a family of string types, and the placeholders are filled by other types.

  2. How does a template literal type split a string into parts?

    Answer

    With infer in a conditional that matches the pattern

    The condition matches the pattern and captures each placeholder with infer.

  3. What is Uppercase<"hey">?

    Answer

    "HEY"

    Uppercase is an intrinsic helper that rewrites the literal to upper case.

  4. Why combine a template literal type with keyof?

    Answer

    To make one listener name for each key of an object type

    The mapped template stamps the pattern once per key, so renaming a key renames its listener too.

  5. What does this program print? ``type EventName<T extends string> = on-${T}; const click: EventName<"click"> = "on-click"; console.log(click);``

    Answer

    on-click

    The template fills click into the pattern, so the only allowed string is on-click.

  6. What does this program print? ``type Props = { title: string; open: boolean; }; type Listener<K extends keyof Props> = change:${K}; const titleChange: Listener<"title"> = "change:title"; console.log(titleChange);``

    Answer

    change:title

    The pattern puts the key after change:, and title is one of the keys.

  7. What does this program print? ``type Split<S extends string> = S extends ${infer H}-${infer Rest} ? [H, Rest] : [S]; const p: Split<"a-b-c"> = ["a", "b-c"]; console.log(p[0], p[1]);``

    Answer

    a b-c

    The first dash splits the head a from the rest b-c, and the tuple keeps both.

  8. What does this program print? type Shout<T extends string> = Uppercase<T>; const loud: Shout<"hey"> = "HEY"; console.log(loud);

    Answer

    HEY

    Uppercase rewrites the literal to HEY, and the constant holds that string.

  9. What does the compiler say about this program? ``type EventName<T extends string> = on-${T}; const bad: EventName<"click"> = "click"; console.log(bad);``

    Answer

    error TS2322: Type '"click"' is not assignable to type '"on-click"'.

    The template type is exactly on-click, so the bare event name does not fit.

  10. What does the compiler say about this program? ``type EventName<T extends string> = on-${T}; type Bad = EventName<number>; const x: Bad = "on-1"; console.log(x);``

    Answer

    error TS2344: Type 'number' does not satisfy the constraint 'string'.

    The placeholder only accepts strings, and number breaks the constraint.

Article author: Arthur Isaev