Lesson 20 — Mapped types
Loop over properties at the type level
A mapped type builds a new type by going through every key of another type: { [K in keyof T]: … }. You can add ? to make every property optional, readonly to lock them, or change each property's type. Partial, Readonly and Record are written exactly this way.
type Flags<T> = { [K in keyof T]: boolean };
type Settings = { dark: string; sound: string };
type SettingsOn = Flags<Settings>; // { dark: boolean; sound: boolean }
Why it matters: you describe a rule once and TypeScript applies it to every property, now and in the future.
Finish Optional<T> as a mapped type, then print draft and draft.done ?? "not set".
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based TypeScript Foundations courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for TypeScript Foundations books — check the publication year so the examples match current versions.
Search Amazon →