What are Mapped Types?
Mapped Types allow you to create a new type based on another type. They use a syntax similar to a
for...in loop in JavaScript to iterate over the keys of an existing type.typescript
Wait! This is how Utility Types like
Partial and Readonly are built under the hood.Mapping Modifiers
You can add or remove modifiers like
? (optional) or readonly during the mapping process using + or -.typescript
Key Remapping (as)
Since TypeScript 4.1, you can re-map keys during the transformation using the
as clause and Template Literal Types.typescript
Why use Mapped Types?
- Safety: Ensure that multiple types stay in sync with a single "Source of Truth."
- Transformation: Create complex versions of types (like Getters/Setters) automatically.
- Generics: Build powerful reusable utility types for your codebase.
Summary
| Term | Syntax | Description |
|---|---|---|
| keyof | keyof T | Getting a union of all keys in a type |
| in | [P in K] | Iterating through keys |
| -readonly | Modifier | Removing immutability |
| +? | Modifier | Adding optionality |
| as | Key Remapping | Changing the name of keys during mapping |
| Capitalize | Helper | Built-in type for changing case |
| Source | Single Truth | Change one interface, and all mapped types update |
| Best For | Store logic, API clients, form validation | |
| Logic | "Functional programming for types" | |
| Modern | The secret sauce behind most advanced TS libraries | |
| Safety | Prevents forgetting to update related types manually | |
| Constraint | Mapped types only work with fixed sets of keys |