Mapped Types in TypeScript Explained with Examples

Loop over keys to build a new object type, and add or remove readonly and ? with + and - modifiers. How Partial and Readonly are built.

In this chapter

Mapped types

More on keyof, indexed, mapped and conditional types

Keep learning

Overview

chapters · keyof, indexed, mapped and conditional types Mapped types Loop over keys to build a new object type. Add or remove readonly and ? with modifiers.

1. The problem: a twin type that forgets a field

A booking form needs a "patch" type: every field optional. Writing it by hand means keeping two lists in sync. Plain cause: the object has a key (roomType) that BookingPatch does not list. TypeScript rejects unknown keys in a fresh object literal, so a valid patch is refused. mapped from Booking theme is now required, and undefined is gone from it. readonly is kept. Modifiers combine.

5. Change the value type too

The right side can be any type that uses K or T[K]. Here, one validator per form field. v: string v: number Plain cause: the mapped type wants a rule for every key, and terms has none. Also note v needs no annotation: for email, T[K] is string, so TypeScript works out v: string. Add a field to the form and the compiler sends you here to write its rule.

6. keyof T keeps modifiers; a plain union does not

A map over keyof T is called “homomorphic” (a fancy word for “keeps the same shape”). It copies readonly and ? from T. A map over a hand-written union of keys has no T to copy from. [K in keyof T] keeps ? from T keeps readonly from T arrays / tuples stay arrays Pick (keep only some keys), Partial and Readonly all loop over keyof T (or a subset of it) so they keep modifiers. What is B?

Remember

T[K] on the right reads the original value type. ?, -?, readonly, -readonly add or strip modifiers. A mapped type forces you to handle every key: great for lookup tables. ! Cheat code → Go deeper: key remapping with as, Partial, Required, Pick, Omit, Record, build your own utility types, implement Pick, Omit and DeepReadonly.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.