Type Parameters in Mapped Types
What you'll learn: How to make mapped types generic so they can transform any input type, not just hardcoded ones.
The Problem with Hardcoded Mapped Types
So far, you've been creating mapped types that work on specific types. But what if you want a transformation that works on any object type you pass in? That's where type parameters come in.
Making Mapped Types Generic
A generic mapped type uses a type parameter (like T) to accept any input type, then transforms it. This makes your mapped type reusable across your entire codebase.
type MyPartial<T> = {
[K in keyof T]?: T[K];
};
interface User {
name: string;
age: number;
}
interface Product {
title: string;
price: number;
}
// Same mapped type works on different inputs
type PartialUser = MyPartial<User>;
// { name?: string; age?: number; }
type PartialProduct = MyPartial<Product>;
// { title?: string; price?: number; }
The Pattern
The type parameter T flows through the mapped type:
keyof Titerates over the keysT[K]looks up each property's type- The transformation applies to whatever type you pass in
This is exactly how TypeScript's built-in Partial<T>, Readonly<T>, and Required<T> utilities work — they're all generic mapped types.
Key Takeaway: Type parameters transform mapped types from one-off helpers into reusable type transformations that work on any compatible input type.