TypeScript for real code · Chapter 21 of 36

Types that decide

By the end of this chapter you can write a type that checks another type and picks an answer, filter a union with it, and pull a type out of a bigger one with infer. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Types that compute types
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.

Why you need this

Kabir is reading the source of a library Tapri uses, and Bunty looks at a line full of extends, ? and infer: "That is not code, that is a riddle." It is an if statement for types, and the toolbox from chapter 17 is built from it: ReturnType, Awaited, Exclude.

Tapri needs one of its own, too. Ravi's screen lists the chais on the menu and shows whether each has sugar. itemsOfKind(MENU, 'chai') returns a list of every kind of menu entry, whatever kind you ask for, so chai.sugar is an error. Bunty's fix is a cast to Chai[], which compiles just as well after itemsOfKind(MENU, 'snack'), and then prints bun maska, sugar: undefined. And Dev asks every candidate the same question: write ReturnType yourself.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    ElementOf: the element type of any list

    ElementOf<T> is the element type of a list, readonly lists and tuples included, and never for anything that is not a list. ElementOf<string[]> is string, ElementOf<MenuEntry[]> is MenuEntry, ElementOf<readonly ['chai', 'bun']> is 'chai' | 'bun', and ElementOf<number> is never.

    Write it as a conditional type with infer.

  2. Task 2

    Unwrap: the value inside a promise

    Unwrap<T> is the value inside a promise, or T itself when T is not a promise. Unwrap<Promise<number>> is number, Unwrap<Promise<MenuEntry[]>> is MenuEntry[], and Unwrap<string> is string.

    Write it as a conditional type with infer, not with Awaited.

  3. Task 3

    OnlyKind: keep one kind of menu entry

    OnlyKind<T, K> keeps the members of the union T whose kind is K: OnlyKind<MenuEntry, 'chai'> is Chai, OnlyKind<MenuEntry, 'chai' | 'snack'> is Chai | Snack, and OnlyKind<MenuEntry, 'dosa'> is never.

    Then make itemsOfKind generic in the kind: kind must be one of MenuEntry's kinds (so 'dosa' is a type error), and itemsOfKind(MENU, 'chai') has the type Chai[], so you can read .sugar. itemsOfKind(MENU, 'chai') gives the masala chai and the cutting chai, in order, and 'snack' gives the bun maska.

  4. Task 4 · interview question

    Interview: MyReturnType and FirstArg

    Write your own ReturnType, without using the built-in one, and a FirstArg. MyReturnType<() => string> is string, MyReturnType<(qty: number, size: string) => MenuEntry[]> is MenuEntry[], FirstArg<(qty: number, size: string) => void> is number, FirstArg<(entry: MenuEntry) => boolean> is MenuEntry, and both are never for something that is not a function.

    Dev will ask you to explain each part of the type: what extends checks, what infer fills in, and why the parameters are written as they are.

What you will learn

  • Conditional types: T extends U ? X : Y, a choice between two types.
  • Distribution: given a union, the choice is made for each member, so you can filter a union.
  • infer: a type variable that TypeScript fills in by matching a pattern.

More in Types that compute types

Build "Types that decide" yourself

Tapri’s codebase grows, and types keep it honest. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of TypeScript for real code are free. This one comes with the all labs plan.