Study notes · 10% of the exam

Utility, Mapped and Indexed Types

Utility types are small mapped and conditional types from the standard library. Know exactly how each is defined, because their edge cases come straight from those definitions.

Key points

  1. 1

    keyof T lists property keys; T[K] reads property types; T[keyof T] is the union of value types, and T[number] is an array or tuple element type.

  2. 2

    keyof (A | B) is the shared keys only, while keyof (A & B) is every key. A string index signature makes keyof string | number.

  3. 3

    Homomorphic mapped types ({ [K in keyof T]: … }) copy ? and readonly, distribute over unions, and map arrays and tuples to arrays and tuples.

  4. 4

    Change modifiers with +/-: -readonly makes properties writable, -? makes them required and strips the undefined that came from ?.

  5. 5

    Key remapping with as can rename keys and filter them out by remapping to never.

  6. 6

    Omit<T, K> is Pick<T, Exclude<keyof T, K>>: it accepts any key and is not distributive, so it collapses unions. Write a distributive helper when you need one.

  7. 7

    ReturnType and Parameters read the last overload; Awaited unwraps thenables recursively and distributes over unions.

Common traps

  • Readonly and Partial are shallow, and naive deep versions break functions (they lose call signatures) and arrays (elements gain undefined).

  • Omit on a type with a string index signature drops every named property.

  • A typo in Omit<T, "nmae"> compiles silently, while Pick would reject it.

Test yourself on Utility, Mapped and Indexed Types

Ten questions, with the answer and explanation after each one.