TypeScript for real code · Chapter 20 of 36

Types from types

By the end of this chapter you can write a type that walks over every key of another type and builds a new property for each: a new value type, new modifiers, or a new name. 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

Anjali's new "edit dish" screen needs three types made from MenuItem: one where every field may be blank, one with a check for every field, and one with a getter for every field. Bunty wrote all three by hand. Then Meera added veg to MenuItem, and none of the three knew: the screen saved dishes with no veg check at all.

The utility types from chapter 17 build a type from a type, but each one does a single fixed job: Partial adds a question mark, Pick keeps some keys. None of them says "every field, but as a check function". Writing the three by hand means three copies of the field list, which is the drift chapter 17 set out to stop.

What you build: 4 tasks

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

  1. Task 1

    Nullable: every property may be null

    Nullable<T> has the same keys as T, and each property may also be null: Nullable<{ name: string; price: number }> is exactly { name: string | null; price: number | null }.

    An optional property stays optional and a readonly one stays readonly: Nullable<{ note?: string }> is { note?: string | null }, and Nullable<{ readonly id: string }> is { readonly id: string | null }. Write it as a mapped type over the keys of T.

  2. Task 2

    Mutable and Complete: take modifiers away

    Mutable<T> removes readonly from every property and leaves optional ones optional: Mutable<{ readonly id: string; readonly price: number }> is { id: string; price: number }, and Mutable<{ readonly note?: string }> is { note?: string }.

    Complete<T> makes every property required and leaves readonly ones readonly: Complete<{ name?: string; price?: number }> is { name: string; price: number }, and Complete<{ readonly id?: string }> is { readonly id: string }. Write both as mapped types, not with the built-in utility types.

  3. Task 3

    Getters: a getter for every property

    Getters<T> has a getter function for each property, named get plus the property name with a capital first letter, and nothing else. Getters<{ name: string; price: number }> is exactly { getName: () => string; getPrice: () => number }, and the getter for veg: boolean returns a boolean.

    A key that is not a property's getter, like getColour, is a type error.

  4. Task 4 · interview question

    Interview: Validators, a check of each field's own type

    Validators<T> gives each field of a form a check function that takes that field's own type and returns an error message or null: Validators<OrderForm>['qty'] is (value: number) => string | null. Every field needs a check, and a qty check that takes text is a type error.

    Give orderRules the type Validators<OrderForm>, and make validate generic: it takes a form of any type T and rules of type Validators<T>, and returns a list of strings. validate({ customer: ' ', qty: 30, takeaway: true }, orderRules) is ['customer is empty', 'qty must be 1 to 20'], and a valid form gives []. For a form { name: 'chai', price: 40 }, a price check is told its value is a number, so reading its length is a type error. Dev will ask how a type can make the rules fit the form.

What you will learn

  • Mapped types: { [K in keyof T]: ... }, a loop over the keys of a type that makes one property for each.
  • Modifiers: adding or taking away readonly and ? on every property at once.
  • Renaming keys with as: a new name for every key, built from the old one.

More in Types that compute types

Build "Types from types" 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.