Key remapping with as
Rename, filter and generate keys inside a mapped type: getters, setters and event handlers. A free TypeScript lesson with examples.
More on keyof, indexed, mapped and conditional types
Keep learning
Overview
chapters · keyof, indexed, mapped and conditional types Key remapping with as Rename, filter and generate keys inside a mapped type: getters, setters and event handlers.
1. The problem: names derived by hand
A chat store exposes one getter per field. The getter type is written by hand, so a new field gets no getter. getters listed by hand This file compiles. The gap shows later: g.getTyping() fails with “Property 'getTyping' does not exist on type 'ChatGetters'”. getters generated from the state
2. The as clause: loop over one key, emit another
K in keyof T loop over old keys as NewKey compute the new name value still uses old K K keeps the original key on the right side. Only the emitted name changes. in a type is a template literal type: it glues strings together, like a JS template string, but for types.
Getters , key by key
keyof ChatState "room" | "unread" | "typing" repeat for "unread" and "typing"
3. Why string & K?
keyof T can include numbers and symbols (a symbol is a special unique key, not a string). Capitalize only takes strings. The rule “only strings allowed” is called a constraint. Plain cause: T could be any type, so K could be a number key. A number doesn't fit where only strings are allowed. The fix string & K means “the part of K that is also a string”. For a symbol key, nothing is both, so it becomes never (the empty type). Capitalize<string & K> symbol keys become never and vanish Capitalize<K & string> same thing, other order [K in keyof T & string as ...] filter before the loop
DataOnly : the as clause per key
no → keep "id" no → keep "total" yes → never (dropped) as clause as Exclude<K, 'id'> as T[K] extends string ? K : never only string fields as K extends ? never : K
5. Generate an API from one shape
From Profile Like useState pairs, generated for every field. change handlers Optional props for a form component, typed per field. loading flags The value need not use T[K] at all.
6. Loop over a union of objects
The loop variable can be a whole object type. Use as to pick the key from it. So each handler gets the exact event for its key. No if (e.type === …) check (narrowing) is needed inside it.
Remember
Wrap with string & K before Capitalize or a template. A key mapped to never disappears: that is how you filter. You can loop over a union of objects and key by E['type']. Remapping over keyof T keeps ? and readonly. ! Cheat code as renames. as Cond ? never : K filters. [E in Union as E['type']] builds a handler map. → Go deeper: mapped types, template literal types, conditional types, type-safe event emitters.
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.