Indexed Access Types
An indexed access type reads the type of a property, written T[K]. K must be a key of T, and the result is the type stored at that key.
Detailed Explanation
JavaScript already lets you write user["email"]. Indexed access is the type-level version of that read. User["email"] is string if User has email: string. The brackets take a type, not a value: a string literal type, a number literal type, or a union of keys.
The useful form is a union of keys. User["id" | "email"] is string | string, which simplifies to string. User[keyof User] is the union of every property type on User. That pattern — (typeof list)[number] for arrays, T[keyof T] for objects — is how you extract a union from a table of values.
K can be a type parameter. function get<T, K extends keyof T>(obj: T, key: K): T[K] is the canonical example. The return type depends on which key was passed, not on a single widened union, as long as K stays a parameter instead of being written as keyof T in the return position.
Optional properties become unions with undefined: if nickname?: string, then User["nickname"] is string | undefined. Readonly does not change the accessed type. An index signature contributes its value type when you access with string or number.
You can nest. User["address"]["city"] walks into an object type. You can combine with conditional types to walk optional paths, which is how libraries type lodash.get and form libraries type dotted paths. That nesting is still erased. There is no runtime lookup happening in the type.
A common mistake is using a value in the brackets. User[key] where key is a variable is a value expression, not a type. In a type position you need User[typeof key] or, more often, a generic K. Another mistake is accessing with string when you meant a specific key: User[string] only works if User has a string index signature, and the result is that signature's value type, not a union of known properties.
Indexed access is how Pick and Omit are built, how event maps look up a payload from an event name, and how a Prisma-style client types findMany results from a model name. If you find yourself writing as after obj[key], the missing piece is usually K extends keyof T plus a T[K] return.
Code Examples
1. Looking up one property and a union of properties.
// 1. Looking up one property and a union of properties.
type User = {
id: string;
age: number;
nickname?: string;
};
type Id = User["id"]; // string
type IdOrAge = User["id" | "age"]; // string | number
type Nickname = User["nickname"]; // string | undefined
type AnyField = User[keyof User]; // string | number | undefined2. The return type follows the key that was passed.
// 2. The return type follows the key that was passed.
function pluck<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const user: User = { id: "u1", age: 36 };
const id = pluck(user, "id"); // string
const age = pluck(user, "age"); // number3. Extracting a union from an array of objects.
// 3. Extracting a union from an array of objects.
const statuses = [
{ code: "idle", retryable: false },
{ code: "error", retryable: true },
] as const;
type StatusCode = (typeof statuses)[number]["code"];
// "idle" | "error"Common Gotchas
`T["missing"]` is a type error
the key must exist, or you must use a constraint. A runtime `obj[key]` can still be `undefined` even when the type says the key exists.
`T[string]` needs an index signature
without one, accessing with `string` fails. Do not add a wide index signature just to make this compile; you will lose excess-property checking.
`T[K]` on an optional key includes `undefined`
handle it, or exclude optional keys before indexing.
Using `keyof T` as the return, instead of `T[K]`, widens too much
`function f<T>(obj: T, key: keyof T): T[keyof T]` returns the union of all values, not the one you picked.