← Lessons

quiz vs the machine

Platinum1800

Frontend

Mapped Types

Transform every property of a type with one rule.

5 min read · advanced · beat Platinum to climb

Looping over keys

A mapped type walks each key of an existing type and produces a new property for it. The form reads as for each key K in keyof T, build a property. It is the engine behind many utility types.

Modifiers

You can add or remove property modifiers as you map.

  • Add or strip optional with a question mark prefix.
  • Add or strip readonly with a readonly prefix.
  • A minus sign removes a modifier the source had, letting you build a Required helper.

Key remapping with as

Using an as clause you can rename or filter keys while mapping. Returning never for a key drops it entirely, which lets you exclude keys by name or by their value type.

Combining with conditionals

Map a type and use a conditional on each property value to decide its new type. This pairing builds powerful transforms, like making only function valued properties optional.

Why it matters

Mapped types keep derived shapes in lockstep with their source. Change the base type and every mapped result updates, so you never hand maintain a parallel type.

Key idea

A mapped type rebuilds a type key by key, adjusting modifiers and even renaming keys with as.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a minus sign before a modifier do in a mapped type?

2. How can a mapped type drop a key while mapping?