What Are Indexed Access Types?
Indexed access types let you look up the type of a property or element using another type.
Their basic syntax is T[K], where
T is the type being accessed and
K is a valid key or index type.
This is similar in concept to accessing a property in JavaScript, but indexed access types operate at compile time. They let you reuse the type of an existing property instead of manually declaring the same type again.
type User = {
id: number;
name: string;
active: boolean;
};
type UserName = User["name"];
// string
type UserId = User["id"];
// number
Indexed access types are especially useful when building reusable functions, API models, configuration utilities, and complex type transformations.
How Does T[K] Work?
The expression T[K] asks TypeScript for the type
associated with key K in type
T. The key must be valid for the type being indexed.
type Product = {
title: string;
price: number;
available: boolean;
};
type TitleType = Product["title"];
type PriceType = Product["price"];
type AvailabilityType = Product["available"];
// string
// number
// boolean
If you try to access a property that is not part of the type, TypeScript reports an error rather than silently inventing a property type.
type Product = {
title: string;
price: number;
};
// Compile-time error:
// type UnknownType = Product["description"];
How Do Indexed Access Types Work with keyof?
Combining keyof with indexed access types lets you
obtain a union of the types of all properties in an object.
type User = {
id: number;
name: string;
active: boolean;
};
type UserKey = keyof User;
// "id" | "name" | "active"
type UserValue = User[keyof User];
// number | string | boolean
First, keyof User produces a union of valid keys.
Then User[keyof User] looks up the type for every
key in that union and combines the results into a union of property value types.
This pattern is useful when you need a type representing any value that can be stored in the properties of an object.
How Do You Use Indexed Access Types with Generics?
Indexed access types become particularly powerful when the key is a generic type parameter. They allow a function's return type to depend on which property is selected.
function getProperty(
object: T,
key: K
): T[K] {
return object[key];
}
const settings = {
theme: "dark",
fontSize: 16,
compactMode: true
};
const theme = getProperty(settings, "theme");
// string
const fontSize = getProperty(settings, "fontSize");
// number
const compactMode = getProperty(settings, "compactMode");
// boolean
The generic constraint ensures the key belongs to the object type. The indexed access return type then preserves the property-specific result.
This is more precise than returning string | number | boolean
for every call, because TypeScript can infer the selected property type from the argument.
Can You Access Multiple Properties at Once?
Yes. An indexed access type can use a union of keys. The result becomes a union of the property types corresponding to those keys.
type User = {
id: number;
name: string;
email: string;
active: boolean;
};
type ContactFields = User["name" | "email"];
// string
type DisplayFields = User["name" | "id"];
// string | number
When multiple keys share the same property type, the resulting union can simplify to
a single type. For example, both name and
email are strings, so their combined result is
simply string.
How Do Indexed Access Types Work with Arrays?
Indexed access types can retrieve an array's element type using
number as the index type.
type Names = string[];
type Name = Names[number];
// string
type Scores = number[];
type Score = Scores[number];
// number
This is especially useful when the array is declared elsewhere and you want to derive the element type without duplicating its declaration.
It also works with tuples. A tuple's numeric indexed access type produces a union of its element types.
type UserRecord = [number, string, boolean];
type UserRecordValue = UserRecord[number];
// number | string | boolean
If you need the type at one particular tuple position, you can index it with a specific numeric literal type.
type UserRecord = [number, string, boolean];
type RecordId = UserRecord[0];
// number
type RecordName = UserRecord[1];
// string
How Do You Derive a Type from a Constant Object?
When an object is declared with as const, you can
derive a union of its exact values using typeof,
keyof, and indexed access.
const roles = {
admin: "ADMIN",
editor: "EDITOR",
viewer: "VIEWER"
} as const;
type RoleKey = keyof typeof roles;
// "admin" | "editor" | "viewer"
type Role = typeof roles[RoleKey];
// "ADMIN" | "EDITOR" | "VIEWER"
function assignRole(role: Role): void {
console.log(role);
}
assignRole("ADMIN");
// Compile-time error:
// assignRole("OWNER");
This technique is useful for status values, roles, event names, feature flags, and other fixed sets of values. It keeps the object and its type synchronized.
How Do Indexed Access Types Work with Optional Properties?
When you access the type of an optional property, the result includes
undefined because the property may be absent.
type User = {
id: number;
nickname?: string;
};
type Nickname = User["nickname"];
// string | undefined
This accurately represents the possibility that the property has no value. If the property is read from an object at runtime, application code should account for that possibility before using the value as a definite string.
function displayNickname(user: User): string {
return user.nickname ?? "Guest";
}
How Do Indexed Access Types Work with Nested Objects?
You can chain indexed access types to reach a nested property type. Each level uses the type of the previous property as the next type to index.
type ApiResponse = {
data: {
user: {
id: number;
name: string;
};
};
success: boolean;
};
type Data = ApiResponse["data"];
type User = ApiResponse["data"]["user"];
type UserName = ApiResponse["data"]["user"]["name"];
// Data: { user: { id: number; name: string } }
// User: { id: number; name: string }
// UserName: string
This is useful when working with deeply nested API response models. It allows one type to reuse the structure of another without repeating the same object definition.
For deeply nested or highly dynamic data, keep readability in mind. Named intermediate type aliases can make complex declarations easier to maintain.
How Do Indexed Access Types Work with Utility Types?
Utility types such as Pick and
Omit often use key constraints, while indexed access
types can retrieve the exact type of a selected property. Together, they help build
reusable transformations.
type Product = {
id: number;
title: string;
price: number;
active: boolean;
};
type ProductSummary = Pick;
type ProductPrice = Product["price"];
type ProductFields = Product[keyof Product];
// ProductSummary: { id: number; title: string }
// ProductPrice: number
// ProductFields: number | string | boolean
The key distinction is that Pick creates a new object
type containing selected properties, while indexed access extracts the type of a property
or a union of property value types.
Indexed Access Types vs Runtime Property Access
These two expressions may look similar, but they operate at different levels:
const user = {
id: 42,
name: "Maya"
};
const nameValue = user["name"];
// Runtime value: "Maya"
type UserName = typeof user["name"];
// Static type: string
The expression user["name"] accesses an actual
property value at runtime. The type expression
typeof user["name"] derives its static type.
For a declared object type, User["name"] directly
expresses the indexed access type.
TypeScript removes type annotations and type-level expressions from emitted JavaScript. Indexed access types do not add runtime checks or create runtime values.
Common Indexed Access Type Mistakes
Using an invalid key
The key must be valid for the indexed type. Use keyof when you need to derive the valid key union instead of manually repeating property names.
Confusing keys with property values
keyof T produces a key type. T[keyof T] produces a union of the corresponding property value types.
Ignoring undefined
Optional properties contribute undefined to their indexed access type. Handle absent values when using the property.
Expecting runtime validation
Indexed access types exist at compile time. They do not verify that incoming JSON actually matches the declared structure.
Best Practices for Indexed Access Types
- Use
T[K]to derive property value types rather than duplicating declarations. - Combine
keyof Twith generic constraints for safe property access. - Use
T[keyof T]when you intentionally need a union of property value types. - Use
T[number]to derive array or tuple element types. - Account for
undefinedwhen indexing optional properties. - Extract complicated nested types into named aliases to improve readability.
Final Takeaway
Indexed access types use T[K] to retrieve a property
or element type from another type. They are most useful when combined with
keyof, generics, arrays, tuples, and
typeof.
By deriving types from existing structures, you can create reusable utilities and keep related types consistent. Remember that indexed access types provide compile-time information, not runtime validation.