Common Mapped Type Patterns
You'll learn: The standard patterns that appear over and over when working with mapped types, and how to recognize them instantly.
Why Patterns Matter
By now you've seen how to build mapped types. But in real code, certain patterns show up repeatedly. Recognizing these patterns helps you understand complex types at a glance and know when to reach for mapped types in your own code.
The Core Patterns
Property Wrapping
Wrapping each property's type in a container type:
type Boxed<T> = {
[K in keyof T]: { value: T[K] }
};
type User = { name: string; age: number };
type BoxedUser = Boxed<User>;
// { name: { value: string }; age: { value: number } }
Nullability Changes
Making properties nullable or removing nullability:
type Nullable<T> = {
[K in keyof T]: T[K] | null
};
type NonNullable<T> = {
[K in keyof T]: Exclude<T[K], null | undefined>
};
Type Transformation
Converting all properties to a different type entirely:
type StringifyAll<T> = {
[K in keyof T]: string
};
Spotting Patterns in the Wild
When you see [K in keyof T], look at what happens to T[K]:
- Preserved (
T[K])? It's about modifiers (optional, readonly) - Wrapped (
Something<T[K]>)? It's property wrapping - Unioned (
T[K] | X)? It's nullability or extension - Replaced (
stringinstead ofT[K])? It's transformation
Key Takeaway: Most mapped types fall into a few patterns—property wrapping, nullability changes, and type transformation. Recognizing these patterns helps you read and write complex types faster.