C.W.K.
Stream
Lesson 03 of 06 · published

Indexed Access: `T[K]`

~8 min · type-manipulation, indexed-access, type-extraction

Level 0Curious
0 XP0/93 lessons0/23 achievements
0/100 XP to next level100 XP to go0% complete
"T[K] reads a property type the same way obj[key] reads a value."

The indexed-access type operator

T[K] evaluates to the type of T's property at key K. { id: number; name: string }['id'] is number. { id: number; name: string }['name'] is string. The syntax mirrors runtime property access; the difference is that this happens at the type level.

K can be a single key or a union of keys: User['id' | 'name'] is number | string. The result is the union of value types at those keys.

Numeric indexed access for arrays

string[][number] is string — the element type of a string array. [number, string][0] is number — the first position of a tuple. [number, string][number] is number | string — the union of all positions.

For tuples, integer literal access gives you the type at that position. For arrays, [number] gives the element type. This is the canonical way to talk about array elements at the type level.

The keyof + indexed access pattern (revisited)

T[keyof T] is the union of all value types in T. This pairing is so common that it has a name in the docs: 'indexed access through keyof.' Combined with mapped types, it's how most utility types navigate object shapes.

Indexed access is the type-level cousin of property access. Once you can read T[K], most advanced TypeScript type definitions become navigable.

Code

Indexed access — single, union, array, tuple·typescript
interface User { id: number; name: string; email: string }

// Single key.
type UserId = User['id'];         // number
type UserName = User['name'];     // string

// Union of keys.
type UserIdOrName = User['id' | 'name'];  // number | string
type AllValues = User[keyof User];        // number | string (same here)

// Tuples — positional indexed access.
type Pair = [number, string];
type First = Pair[0];             // number
type Second = Pair[1];            // string
type Any = Pair[number];          // number | string (all positions)

// Arrays — element type via [number].
type Element = User[][number];    // User
type FirstUser = User[][0];       // User (any non-negative integer also works)

External links

Exercise

Given type Vehicle = { make: string; year: number; wheels: 2 | 4 }, extract each field's type via indexed access. Then derive VehicleValues = Vehicle[keyof Vehicle]. Compare what you get with hovering each manually.
Hint
Each Vehicle['make'], Vehicle['year'], etc. matches the field types. The keyof+indexed access pair gives you string | number | (2 | 4) — which the compiler may simplify to string | number | 2 | 4.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.