Module 6 · Lesson 2 of 212/22 in the course~18 min
Module lessons (2/2)
Mapped Types
Lesson preview5 hands-on exercises readyCourse TypeScript · Module 6 — Conditional and Mapped Types
Generate derived interfaces with { [K in keyof T]: ... }, key remapping as and modifiers +/-readonly.
What you'll learn in this lesson
- Systematically iterate over all keys of an object
Tthrough[K in keyof T]. - Understand how
Partial<T>,Required<T>,Readonly<T>and the+/-(-readonly,-?modifiers are constructed internally. - Rename or filter keys using the
asremapping clause (e.g.[K in keyof T as ...]) withCapitalizeand conditional types. - Because writing
string & Kis essential when interpolatingkeyof Tinto a template literal type.
Overview
If the Utility Types (Partial, Readonly, Pick) offer you ready-made transformations, the Mapped Types give you the engine with which those same utilities are implemented inside TypeScript: a loop over all the keys of T.
By pressing -readonly or -? you can remove constraints inherited from the source type!
Sections covered in this lesson
- 1Syntax
{ [K in keyof T]: ... }and Modifiers+/- - 2Key Remapping with
as - 3Guided practice
- 4Check for understanding
Pro Lesson · Module 6 — Conditional and Mapped Types
Unlock "Mapped Types" and the full Course TypeScript curriculum
Generate derived interfaces with { [K in keyof T]: ... }, key remapping as and modifiers +/-readonly.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course TypeScript plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.