Mapped Type (TypeScript)

A mapped type builds a new type by transforming each property of an existing one, using a `[K in keyof T]` loop in the type system. It is how you generate types programmatically.

TypeScript

Mapped types let you take a type and produce a variant of it — making every property optional, readonly, or nullable — without rewriting each member by hand. Many of TypeScript's built-in utility types are mapped types under the hood.

They are an advanced feature, but understanding them demystifies utilities like `Partial`, `Readonly`, and `Record`, which all map over keys.

Syntax

type T = { [K in keyof Source]: NewType };

Examples

TypeScript

type Optional<T> = { [K in keyof T]?: T[K] };

interface User { name: string; age: number; }
type MaybeUser = Optional<User>;
// { name?: string; age?: number }

const u: MaybeUser = { name: "Ana" };
console.log(u.name);

Output:

Ana

Learn it in a lesson

See also

Back to the Dictionary