Was this page helpful?

Indexed Access Types

We can use anindexed access type to look up a specific property on another type:

ts

The indexing type is itself a type, so we can use unions,keyof, or other types entirely:

ts
typeI1 =Person["age" |"name"];
type I1 = string | number
 
typeI2 =Person[keyofPerson];
type I2 = string | number | boolean
 
typeAliveOrName ="alive" |"name";
typeI3 =Person[AliveOrName];
type I3 = string | boolean
Try

You’ll even see an error if you try to index a property that doesn’t exist:

ts
typeI1 =Person["alve"];
Property 'alve' does not exist on type 'Person'.2339Property 'alve' does not exist on type 'Person'.
Try

Another example of indexing with an arbitrary type is usingnumber to get the type of an array’s elements.We can combine this withtypeof to conveniently capture the element type of an array literal:

ts
constMyArray = [
{name:"Alice",age:15 },
{name:"Bob",age:23 },
{name:"Eve",age:38 },
];
 
typePerson =typeofMyArray[number];
type Person = { name: string; age: number;}
typeAge =typeofMyArray[number]["age"];
type Age = number
// Or
typeAge2 =Person["age"];
type Age2 = number
Try

You can only use types when indexing, meaning you can’t use aconst to make a variable reference:

ts
constkey ="age";
typeAge =Person[key];
Type 'key' cannot be used as an index type.
'key' refers to a value, but is being used as a type here. Did you mean 'typeof key'?
2538
2749
Type 'key' cannot be used as an index type.
'key' refers to a value, but is being used as a type here. Did you mean 'typeof key'?
Try

However, you can use a type alias for a similar style of refactor:

ts
typekey ="age";
typeAge =Person[key];
Try

The TypeScript docs are an open source project. Help us improve these pagesby sending a Pull Request

Contributors to this page:
OTOrta Therox  (5)

Last updated: Dec 16, 2025