Indexed Access Types
We can use anindexed access type to look up a specific property on another type:
tsTrytypePerson = {age :number;name :string;alive :boolean };typeAge =Person ["age"];
The indexing type is itself a type, so we can use unions,keyof, or other types entirely:
tsTrytypeI1 =Person ["age" |"name"];typeI2 =Person [keyofPerson ];typeAliveOrName ="alive" |"name";typeI3 =Person [AliveOrName ];
You’ll even see an error if you try to index a property that doesn’t exist:
tsTrytypeProperty 'alve' does not exist on type 'Person'.2339Property 'alve' does not exist on type 'Person'.I1 =Person ["alve" ];
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:
tsTryconstMyArray = [{name :"Alice",age :15 },{name :"Bob",age :23 },{name :"Eve",age :38 },];typePerson =typeofMyArray [number];typeAge =typeofMyArray [number]["age"];// OrtypeAge2 =Person ["age"];
You can only use types when indexing, meaning you can’t use aconst to make a variable reference:
tsTryconstkey ="age";typeType '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'?2538Age =Person []; key
2749Type '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'?
However, you can use a type alias for a similar style of refactor:
tsTrytypekey ="age";typeAge =Person [key ];
The TypeScript docs are an open source project. Help us improve these pagesby sending a Pull Request ❤
Last updated: Dec 16, 2025