`T[K]` — Look Up a Type by Key
Indexed Access Types
`T[K]` extracts the type of T's property K. Foundational for building types that follow other types.
What you'll learn
- Read indexed-access syntax
- Use it to derive types
- Recognize array-element types
T[K] accesses a type’s property by key — at the type level.
A Single Key
type User = { name: string; age: number };
type Name = User["name"]; // string
type Age = User["age"]; // number User["name"] is the type of User’s name field — string.
Multiple Keys (Union)
type StringOrNumber = User["name" | "age"]; // string | number A union of keys gives a union of value types.
All Keys With keyof
type Values = User[keyof User]; // string | number User[keyof User] is the union of every value type. Useful for
“some value from this object’s properties”.
Array Element Types
type Tags = string[];
type Tag = Tags[number]; // string Tags[number] says “the type at any number index” — the element
type.
For tuples, T[0] and T[1] give specific positions.
A Useful Pattern — Drive Types From Const Data
const ROUTES = ["/", "/about", "/blog"] as const;
type Route = typeof ROUTES[number];
// "/" | "/about" | "/blog" as const keeps the literal types; [number] extracts the union
of elements. A small, powerful combo.
Object Values
const STATUS = {
ok: 200,
notFound: 404,
error: 500,
} as const;
type StatusCode = typeof STATUS[keyof typeof STATUS]; // 200 | 404 | 500 A bit dense. Reading it inside out:
typeof STATUS—{ readonly ok: 200; readonly notFound: 404; readonly error: 500 }keyof typeof STATUS—"ok" | "notFound" | "error"typeof STATUS[keyof typeof STATUS]—200 | 404 | 500
A common idiom for deriving enum-like types from a const object.
Up Next
Building new types by walking a type’s properties — mapped types.
Mapped Types →