What are Mapped Types in TypeScript?
Master TypeScript mapped types: iterate keys with keyof, add or remove readonly and optional modifiers, and remap keys with 'as' — with code examples.
Expected Interview Answer
Mapped types are a way to create a new type by iterating over the keys of an existing type and transforming each property, using the syntax { [K in keyof T]: ... }. They are the mechanism behind built-in utilities like Partial, Readonly, and Record.
A mapped type walks every key K in a union (usually keyof T) and defines the resulting property for that key, optionally changing its type with T[K] and adjusting modifiers. You can add or remove optionality and readonly with the +/- prefixes (e.g. -readonly, -?), and remap or filter keys with the 'as' clause plus conditional types. This lets you express whole families of related types from a single source shape.
- Generate types programmatically from a source
- Add or strip readonly and optional modifiers
- Remap and filter keys with 'as' clauses
- Power reusable utilities across a codebase
- Keep derived shapes perfectly in sync
AI Mentor Explanation
Think of a stamp a scorer runs down every name on the team sheet, applying the same rule to each entry — say, marking every player 'available: optional'. A mapped type is that stamp for a type: it visits each key in turn and applies one transformation rule, producing a fresh sheet where every field has been reshaped identically.
Step-by-Step Explanation
Step 1
Get the key union
Use keyof T to obtain the union of property names you want to iterate over.
Step 2
Iterate with 'in'
Write { [K in keyof T]: ... } so K takes each key in turn.
Step 3
Define each property
Set the value type, often via T[K], and adjust it as needed.
Step 4
Tune modifiers
Add or remove readonly and optional using +/- prefixes, e.g. -readonly or -?.
Step 5
Remap keys optionally
Use an 'as' clause with template literals or conditional types to rename or filter keys.
What Interviewer Expects
- Knows the { [K in keyof T]: ... } syntax
- Understands T[K] indexed access lookups
- Can add/remove readonly and optional with +/-
- Aware of key remapping via 'as' clauses
- Connects mapped types to Partial/Readonly internals
Common Mistakes
- Forgetting keyof and iterating the type itself
- Not knowing modifiers can be removed with -readonly / -?
- Confusing indexed access T[K] with an array index
- Assuming key remapping requires a separate type helper
- Overlooking that homomorphic mapped types preserve modifiers
Best Answer (HR Friendly)
“Mapped types let TypeScript build a new type by looping over the fields of an existing one and applying the same change to each — like making every field optional or read-only. They are the engine behind many of TypeScript's built-in type helpers.”
Code Example
// Reimplementing Readonly with a mapped type
type MyReadonly<T> = {
readonly [K in keyof T]: T[K]
}
// Strip readonly and optional modifiers
type Mutable<T> = {
-readonly [K in keyof T]-?: T[K]
}
interface User {
readonly id: number
name?: string
}
type EditableUser = Mutable<User>
// { id: number; name: string }
// Key remapping: prefix every key with 'get'
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K]
}
type UserGetters = Getters<User>
// { getId: () => number; getName: () => string | undefined }Follow-up Questions
- How do the +/- modifier prefixes work in a mapped type?
- What is a homomorphic mapped type and why does it preserve modifiers?
- How does the 'as' clause enable key remapping?
- How would you filter out keys of a certain type using never?
- How do template literal types combine with mapped types?
MCQ Practice
1. What is the correct syntax to iterate a type's keys in a mapped type?
Mapped types use { [K in keyof T]: ... }, where 'in' iterates the union of keys produced by keyof T.
2. Which modifier syntax removes optionality from every property?
The -? modifier strips the optional flag, making previously optional properties required.
3. What does the 'as' clause in a mapped type enable?
The 'as' clause remaps each key to a new name (often via template literals) or filters keys by mapping them to never.
Flash Cards
Basic mapped type syntax? — { [K in keyof T]: T[K] } iterates each key K and defines its property.
How to remove readonly and optional? — Use -readonly and -? prefixes on the mapped property.
What is T[K]? — Indexed access — the type of property K within T.
What does 'as' add? — Key remapping and filtering, e.g. renaming keys or dropping them via never.