×

typescript · level 3 · unit 5 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Helper types change the letters of a literal type.

TypeScript has four built-in helpers for literal string types. is "TEA", and Lowercase does the reverse. is "Tea", changing only the first letter, and Uncapitalize turns it back.

They shine inside template literal types. If your fields are "name" | "email", then set${Capitalize<Field>} is "setName" | "setEmail". Add a field and its setter name follows.

type Field = "name" | "email";
type Setter = `set${Capitalize<Field>}`;
type Shout = Uppercase<"stop">;
const setters: Setter[] = ["setName", "setEmail"];
const sign: Shout = "STOP";
console.log(setters, sign);

These helpers only work on types. At runtime you still need .toUpperCase().