Typescript Indexed Access Types
Typescript
We can use an indexed access type to look up a specific property on another type:
type Person = {age: number; name: string; alive: boolean};
type Age = Person['age'];
// type Age = number
The indexing type is itself a type, so we can use unions, keyof, or other types entirely:
type I1 = Person['age' | 'name']; // type I1 = string | number;
type I2 = Person[keyof Person]; // type I2 = string | number | boolean;
type AliveOrName = 'alive' | 'name';
type I3 = Person[AliveOrName]; // type I3 = string | boolean;
const MyArray = [
{name: 'Alice', age: 15},
{name: 'Bob', age: 23},
{name: 'Eve', age: 38},
];
type Person = typeof MyArray[number];
// type Person = {
// name: string;
// age: number;
// }
type Age = typeof MyArray[number]['age'];
// type Age = number
type Age2 = Person['age'];
// type Age2 = number
You can only use types when indexing, meaning you can’t use a const to make a variable reference:
const key = 'age';
type Age = 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'?
However, you can use a type alias for a similar style of refactor:
type key = 'age';
type Age = Person[key];