Mapped Types
Build a new type by looping over every property of an existing type and transforming each one the same way.
What is it?
Utility types like Partial<T> and Record<K, V> aren't compiler special cases. They're built with a feature you can use yourself: the mapped type.
A mapped type looks like an object type, but instead of listing properties it loops over another type's keys, a bit like for...in: { [K in keyof T]: ... }. keyof T is a union of T's property names, and the mapped type creates one property for each name.
That lets you transform every property the same way: add ? to make it optional, wrap it, change its type, or change whether it's readonly.
Explain like I'm 10
A mapped type is like running every item on a checklist through the same rubber stamp. The stamp visits each property in turn and applies the same change ("make it optional," "make it read-only," "wrap it in a box") without you naming the properties by hand.
Examples
Reimplementing Partial and Readonly by hand
interface Task {
title: string;
done: boolean;
}
// This is essentially how the built-in Partial<T> works internally
type MyPartial<T> = {
[K in keyof T]?: T[K];
};
// And how Readonly<T> works
type MyReadonly<T> = {
readonly [K in keyof T]: T[K];
};
type PartialTask = MyPartial<Task>; // { title?: string; done?: boolean }
type ReadonlyTask = MyReadonly<Task>; // { readonly title: string; readonly done: boolean }[K in keyof T] loops over every key of Task ("title" and "done"), and T[K] looks up that property's original type. A ? or readonly applies that modifier to every generated property at once.
Transforming property types, not just modifiers
interface Config {
host: string;
port: number;
debug: boolean;
}
// Turn every property into a function that returns its original type
type Getters<T> = {
[K in keyof T as `get${Capitalize<string & K>}`]: () => T[K];
};
type ConfigGetters = Getters<Config>;
// {
// getHost: () => string;
// getPort: () => number;
// getDebug: () => boolean;
// }Mapped types can also rename keys (with an as clause) and change each property's type entirely. Here every property becomes a zero-argument function returning its original type, under a renamed "getX" key.
How it works
For { [K in keyof T]: ... }, the compiler first resolves keyof T to the union of T's property names (for Task, "title" | "done").
It then goes through that union, binding K to each key in turn, and creates one property per key from the expression after the colon. That's often T[K], an indexed access type that looks up that property's type on T. The optional as clause renames each key.
It all happens at compile time. The result is a fully expanded object type with no loop and no runtime cost.
Why does it exist?
Without mapped types, applying the same change to every property (all optional, all readonly, all wrapped in a function) would mean rewriting the type by hand whenever the original changed, or building a few fixed transformations into the compiler.
Mapped types let anyone write "apply this to every property" once, for any type. That's exactly how Partial, Readonly and Record are built.
When to use it
Use a mapped type when no built-in utility type fits: turning every property into a getter, renaming properties in a predictable pattern, or a reusable domain-specific transformation (like a "validators" object that mirrors a form's fields).
When not to use it
If a built-in utility type (Partial, Pick, Omit, Record, Readonly) already does the job, use it. Readers already know it. Save custom mapped types for what the built-ins don't cover.
Common mistakes
Treating
keyof Tas an array. It's a union of key names, so only union-style operations work on it.Writing
[K in keyof T]: Tinstead of[K in keyof T]: T[K], which gives every property the whole type instead of its own type.Forgetting that renaming keys needs the
asclause. Without it,[K in keyof T]: ...can change values but never key names.
Practice exercises
- Easy:
Write a mapped type
Nullable<T>that turns every property of T intoT[K] | null, keeping it required. - Medium:
Write a mapped type
Stringify<T>that turns every property of T into astring, whatever its original type. - Hard:
Write a mapped type
EventHandlers<T>. T maps event names to payload types. Rename each key toon\${Capitalize<key>}and make each value a function that takes that payload and returns void.
Interview questions
What is a mapped type?
A type that builds a new object type by looping over an existing type's keys (with [K in keyof T]) and applying the same transformation to each property.
What does `keyof T` produce?
A union of the literal names of every property on T.
How are built-in utility types like `Partial<T>` implemented?
As ordinary mapped types in TypeScript's standard type definitions. For example, Partial<T> is { [K in keyof T]?: T[K] }.
What does the `as` clause inside a mapped type let you do that you can't do without it?
Rename each key with a computed expression, for example turning title into getTitle with Capitalize and a template literal type. Without as, a mapped type can only change each property's value; the key is copied straight from the union.
What do the `+` and `-` modifier prefixes mean in a mapped type, and what does no prefix default to?
-readonly and -? remove that modifier from every property; +readonly and +? add it. No sign (readonly or ?) means +. You can only remove modifiers with an explicit -.
Given `type Req<T> = { [K in keyof T]-?: T[K] }`, what does `Req<{ a?: string; b: number }>` resolve to?
{ a: string; b: number }. -? removes optionality, so a becomes required; b already was. This is exactly how Required<T> is implemented.
What makes a mapped type "homomorphic," and why does it matter?
It maps directly over [K in keyof T] for a generic T. The compiler treats it as mirroring T: it copies T's existing readonly/? modifiers (unless overridden with +/-) and keeps array/tuple shapes instead of turning them into plain objects. A mapped type over some other key union gets none of that.
Given `interface Point { readonly x: number; y?: number }`, what does `type Copy<T> = { [K in keyof T]: T[K] }` produce for `Copy<Point>`, even though the mapped type never mentions `readonly` or `?`?
{ readonly x: number; y?: number }, identical to Point. The mapping is homomorphic, so original modifiers are preserved unless the mapped type explicitly writes +/-readonly or ?.
A mapped type written as `{ [K in keyof T]: T }` compiles but produces the wrong result — what's the mistake, and what's the fix?
Every property gets the whole type T instead of its own type. Use T[K] (an indexed access into T at key K) to give each property its original type.
How does a mapped type differ from `Record<K, V>`?
Record<K, V> is itself a mapped type, { [P in K]: V }, giving every key the same value type V. A custom mapped type over keyof T can use each property's own type via T[K], so properties can end up with different types.
Why does `Capitalize<string & K>` include the `string &` intersection instead of just `Capitalize<K>`?
Capitalize needs a string, but K from keyof T is generally string | number | symbol, since keys aren't only strings. string & K keeps just the string part, satisfying the constraint.
Name the built-in string-manipulation utility types mapped types commonly combine with `as`.
Uppercase<S>, Lowercase<S>, Capitalize<S> and Uncapitalize<S>. They change a string literal type's casing at compile time, like their runtime namesakes do to real strings.
How would you write a mapped type `PickByType<T, V>` that keeps only the properties of T whose value type extends V?
type PickByType<T, V> = { [K in keyof T as T[K] extends V ? K : never]: T[K] }. The as clause tests each key; keys whose value doesn't extend V map to never, which drops them from the result.
What does `{ [K in keyof T as never]: T[K] }` produce for any T, and why?
The empty object type {}. Mapping a key to never in as removes it, so doing that for every key removes all of them.
How is a mapped type different from an index signature like `{ [key: string]: number }`?
An index signature is open-ended: any string key maps to number, and names aren't known ahead of time. A mapped type goes through a specific, known set of keys (usually keyof T) and creates exactly one property per key, with fixed, visible names.
Can the `in` clause of a mapped type iterate over a union of string literals that isn't derived from `keyof`? Give an example.
Yes. type Flags = { [K in "draft" | "published" | "archived"]: boolean } gives { draft: boolean; published: boolean; archived: boolean }. in accepts any union of key-like literal types.
How would you write a mapped type that wraps every property's value in `T[K] | null`, without disturbing whatever `readonly`/`?` modifiers the original type already had?
type Nullable<T> = { [K in keyof T]: T[K] | null }. With no +/-/readonly/?, modifiers are inherited from T because the mapping is homomorphic. Only the value type changes.
Why does writing `{ [K in SomeObjectType]: ... }` fail, if `SomeObjectType` is an interface rather than a union of key names?
in needs a union of key-like literal types (string, number or symbol literals), and an object type isn't one. Use keyof SomeObjectType to get the union of its property names first.
You wrote a mapped type to rename every key with a `get` prefix but forgot the `as` clause, writing `{ [K in keyof T]: () => T[K] }` instead. What's wrong with the output, and would TypeScript catch it?
It compiles and wraps every value in a function, but the keys aren't renamed: you get { title: () => string }, not { getTitle: () => string }. TypeScript won't flag it, since that's valid. It's a silent logic bug, not a type error.
Is there really a difference between `Partial<T>` and a hand-written `{ [K in keyof T]?: T[K] }`?
No, they're identical. Partial<T> is defined in the standard library exactly as { [K in keyof T]?: T[K] }.
Does adding `readonly` inside a mapped type do anything to the object at runtime?
No. readonly, ? and the +/- modifiers are compile-time only. They generate no runtime code and don't freeze, seal or otherwise change the JavaScript object.
You need a type where every property becomes a validator function `(value: OriginalType) => boolean`, but the property names stay exactly the same. Do you need the `as` clause?
No. as is only for changing keys. Here only the value changes, so { [K in keyof T]: (value: T[K]) => boolean } is enough.
Can a homomorphic mapped type preserve an array or tuple type instead of turning it into a plain object?
Yes. A homomorphic mapped type ([K in keyof T] over generic T) mirrors whatever T is, so an array or tuple T produces an array/tuple of the same shape with each element type transformed, not an object keyed by indexes.
How does combining a mapped type with a conditional in the `as` clause differ from just using `Pick<T, K>`?
Pick<T, K> needs you to name the keys. A conditional as clause ([K in keyof T as T[K] extends Cond ? K : never]) picks keys dynamically by testing each property's value type, without naming them.