TypeScript Indexed Access Types: T["key"], T[number]
Read a type out of another type with T["key"], get an array element type with T[number], and all value types with T[keyof T].
In this chapter
Indexed access types
More on keyof, indexed, mapped and conditional types
Keep learning
Overview
chapters · keyof, indexed, mapped and conditional types Indexed access types T["key"], T[number] and T[keyof T]: read a type out of another type.
1. The problem: a copy that goes stale
An order status gets a new value, 'disputed'. A helper had copied the old list of statuses by hand. The helper still accepts only the old 3 values, so a valid 'disputed' is rejected at the call. Reminder: 'pending' | 'paid' is a union (“one of these”) of literal types (each allows exactly one string). status retyped by hand status read from Order
3. T[number]: the element type of an array
Arrays are indexed by numbers. Asking for [number] means "any element". typeof SIZES readonly ["s", "m", "l"] index with number: every element the array could get new items later, so 's' is widened to string (typeof B)[0] tuples have exact positions (typeof B)['length'] [string, number][number] string | number
4. T[keyof T]: all the values
Index with every key at once and you get a union of every value type. People call it ValueOf.
type Role = ValueOf
the value keyof T "admin" | "staff" | "guest" "ADMIN" | "STAFF" | "GUEST" Plain cause: Role holds the values ('ADMIN'…), not the keys ('admin'…). Lower-case 'admin' doesn't fit.
5. Mix them: dig into data you already have
typeof MENU value → type one dish its tags array one tag Each bracket goes one level deeper. "Margherita" | "Diavola" "veg" | "spicy" Props of a component API response pieces Library types
6. Predict it
"cart" | "address" | "pay" readonly string[] string | number
Remember
T['key'] reads a property type. Dots do not work on types. A union key gives a union of property types. T[number] is the element type of an array or tuple. T[keyof T] is every value type: ValueOf. ! Cheat code Never retype a piece of a type: index into it. (typeof ARR)[number] for array members. T[keyof T] for all values. → Go deeper: typeof and keyof, mapped types, ReturnType, Parameters, Awaited, enum or union of strings?
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.