Index Access Type T[K]
| Since: | TypeScript 2.1(2016) |
|---|
A feature that extracts a property's type from another type. Using the T[K] notation, you can retrieve the type of the value associated with key K in type T. It is useful for referencing parts of nested types or extracting the element type of an array.
Syntax
type PropertyType = ObjectType["propertyName"]; // Dynamically extract a type using a type parameter. type PropertyType = T[K]; // Requires the constraint K extends keyof T. // Extract all property value types as a union. type AllValueTypes = T[keyof T]; // Extract the element type of an array. type ElementType = ArrayType[number];
Syntax overview
| Syntax | Description |
|---|---|
| T["key"] | Extracts the value type of the "key" property from type T. The key is specified as a string literal. |
| T[K] | Dynamically extracts a property type using the type parameter K. |
| T[keyof T] | Extracts all property value types of type T as a union type. |
| T[number] | Extracts the element type of array type T. Also works with tuple types. |
| T["prop"]["nested"] | Chains index access to reach a nested type. |
Sample code
interface User {
id: number;
name: string;
address: {
city: string;
zipCode: string;
};
}
// Extract property types.
type UserId = User["id"]; // number
type UserName = User["name"]; // string
// Extract a nested type.
type UserAddress = User["address"]; // { city: string; zipCode: string; }
type UserCity = User["address"]["city"]; // string
// Extract multiple property types using a union of keys.
type UserContact = User["name" | "id"]; // string | number
// T[keyof T]: get all property value types as a union.
type AllUserValues = User[keyof User]; // number | string | { city: string; zipCode: string; }
// Extract the element type of an array.
type Colors = ("red" | "green" | "blue")[];
type Color = Colors[number]; // "red" | "green" | "blue"
// Extract the type at a specific position of a tuple.
type Pair = [string, number];
type First = Pair[0]; // string
type Second = Pair[1]; // number
// Practical example: reference a specific field type from an API response.
interface ApiResponse {
status: "success" | "error";
data: {
users: User[];
total: number;
};
error?: {
code: number;
message: string;
};
}
type ResponseData = ApiResponse["data"]; // { users: User[]; total: number; }
type ErrorCode = NonNullable<ApiResponse["error"]>["code"]; // Extracts the type of the code property (number) from the error object.
type UsersList = ApiResponse["data"]["users"]; // User[]
type SingleUser = ApiResponse["data"]["users"][number]; // User
console.log("Compiled index access type examples");
Running the above produces the following output:
npx ts-node ts_index_access.ts Compiled index access type examples
Overview
Index access types use the T[K] notation to extract the type of property K from type T. The syntax looks the same as JavaScript object access (obj["key"]), but the key difference is that it retrieves a type, not a value.
T[keyof T] is a handy pattern for getting all property value types of a type as a union. T[number] is an idiom for extracting the element type of an array type — for example, applying it to string[] yields string.
Index access types are especially useful for referencing only the parts you need from a large API response type. By centralizing type definitions in one place and referencing them elsewhere with index access types, any change to the type definition is automatically reflected everywhere. They are also frequently used in combination with keyof / typeof and mapped types.
If you find any errors or copyright issues, please contact us.