Lesson 14 of 80
The mechanism the utility types lesson's Partial, Pick, and Readonly are actually built from — a way to generate a new object type by transforming every property of an existing one.
13 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
How would you implement your own version of the built-in Partial<T> utility type?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
A mapped type walks every key of an existing type and produces a new type by applying the same transformation to each:
type User = { name: string; age: number };
type ReadonlyUser = { readonly [K in keyof User]: User[K] };
// { readonly name: string; readonly age: number }
type OptionalUser = { [K in keyof User]?: User[K] };
// { name?: string; age?: number }
keyof User gives you the union of User's keys ("name" | "age"); [K in keyof User] loops over that union, one member at a time, and User[K] looks up each property's type — building a brand-new object type, property by property.
type User = { name: string; age: number } — the original shape.
type Concrete<T> = { [K in keyof T]-?: T[K] }; // -? removes optionality
type Mutable<T> = { -readonly [K in keyof T]: T[K] }; // -readonly removes readonly
The - prefix on a modifier (-?, -readonly) strips that modifier instead of adding it — the inverse operations to Partial and Readonly, which is exactly how Required<T> is actually implemented.
keyof plus a mapped type is the standard way to build your own utility type the moment the built-in ones (Partial, Pick, Omit) don't quite fit your specific need.as inside the brackets) lets you rename keys during the mapping: { [K in keyof T as `get${string & K}`]: () => T[K] } turns { name: string } into { getName: () => string } — generating getter method names from property names."How would you implement your own version of Partial<T>?" is a real, common exercise, and mapped types are the honest, correct answer — showing you understand the mechanism the utility types lesson only showed the output of.