Mapped Types
A mapped type builds a new object type by walking the keys of an existing type, written { [K in keyof T]: ... }. Partial, Readonly, and Pick are mapped types.
Detailed Explanation
When every property of a type needs the same transformation — make it optional, make it readonly, wrap it in Promise, replace it with boolean — writing the new type by hand duplicates the keys and then drifts. A mapped type is a loop over keys in the type system.
type Flags<T> = { [K in keyof T]: boolean };
If T is { id: string; name: string }, Flags<T> is { id: boolean; name: boolean }. The in is not the runtime in operator. It iterates a key union. keyof T is the usual source of that union. You can also iterate a literal union you wrote yourself: { [K in "get" | "post"]: Handler }.
Modifiers can be added or stripped. readonly [K in keyof T] adds readonly. -readonly [K in keyof T] removes it. ? makes each property optional; -? makes each required. That is the entire implementation of Partial, Required, and Readonly.
The value side can use K and T[K]. type Nullable<T> = { [K in keyof T]: T[K] | null } keeps each property's own type and adds null. Conditional types inside the mapping let you skip or rewrite some keys. The as clause in a key remapping lets you rename or filter keys: { [K in keyof T as \get${Capitalize<K & string>}`]: () => T[K] }. Filtering is done by remapping a key to never`, which drops it.
Mapped types are homomorphic when they use keyof T directly: they copy modifiers (optional, readonly) from the source unless you override them. That is why Partial<T> on a type with a readonly field stays readonly and optional. Homomorphic mappings also keep the type "the same object, transformed," which helps the compiler relate T and the mapped result.
Two limits. Mapping over a union of object types distributes in ways you may not want — you often wrap the union in a generic so you map the combined shape, or you map each member on purpose. And a mapping that produces a huge key set (template-literal key remapping over a large object) can slow the checker.
There is no runtime loop. A mapped type does not produce an object. You still write the JavaScript that builds the value, and the mapped type describes the result. If you need both, a generic function plus a mapped return type is the usual pair.
Code Examples
1. Transform every property, keep each original type.
// 1. Transform every property, keep each original type.
type User = { id: string; email: string; age: number };
type Nullable<T> = { [K in keyof T]: T[K] | null };
const row: Nullable<User> = { id: "u1", email: null, age: 36 };2. Add and remove modifiers — this is Partial and Required.
// 2. Add and remove modifiers — this is Partial and Required.
type PartialUser = { [K in keyof User]?: User[K] };
type LockedUser = { readonly [K in keyof User]: User[K] };
type RequiredFlags = { [K in keyof PartialUser]-?: PartialUser[K] };
const draft: PartialUser = { email: "ada@example.com" };3. Key remapping: drop keys or rename them.
// 3. Key remapping: drop keys or rename them.
type Getters<T> = {
[K in keyof T as `get${Capitalize<K & string>}`]: () => T[K];
};
type UserGetters = Getters<User>;
// { getId: () => string; getEmail: () => string; getAge: () => number }
type OnlyStrings<T> = {
[K in keyof T as T[K] extends string ? K : never]: T[K];
};
type UserStrings = OnlyStrings<User>; // { id: string; email: string }Common Gotchas
`in` iterates a key union, not a value
`{ [K in User]: ... }` fails unless `User` is a union of keys. Write `{ [K in keyof User]: ... }`.
Mapping a union of objects maps each member separately when the mapping is over a naked type parameter
wrap the type (`type M<T> = { [K in keyof T]: ... }; type X = M<A | B>`) and check whether you wanted one combined shape or a union of shapes.
Remapping a key to `never` deletes it
that is the supported filter. There is no `continue` keyword.
Huge remappings (prefixes × every key × every locale) can stall the checker
split the mapping or reduce the key union.