How does TypeScript's built-in Partial<T> utility type work, using mapped type syntax?
Short answer
Partial is implemented as `{ [K in keyof T]?: T[K] }` — it maps over every key of T and adds a '?' to make each property optional, producing a version of T where every field can be omitted.
Advertisement
AModel answer
Partial<T> is implemented as `{ [K in keyof T]?: T[K] }` — it maps over every key of T and adds a '?' to make each property optional, producing a version of T where every field can be omitted.
interface Person { name: string; age: number; }
type PartialPerson = Partial<Person>;
// { name?: string; age?: number }
function updatePerson(updates: PartialPerson) { /* ... */ }Real-world example
Typing an update/patch function's parameter where callers only need to supply the fields they want to change.
Likely follow-ups
- How would you implement the opposite of Partial — a Required<T> utility type?
Related topics