# Indexed Access Types

> The indexed access types in TypeScript look up the type of a specific property of another type.

## How do I get the type of a property in TypeScript (T['key'])?

The types that get the type of a property by its key, for example `User['id']`. Can take unions of keys or `number` to extract the element type of an array, like `T[number]`.

- Topic: Advanced Types
- Type: concept

## Example

Wrong:

```ts
const roles = ['admin', 'editor'] as const;
type Role = 'admin' | 'editor';
```

Right:

```ts
const roles = ['admin', 'editor'] as const;
type Role = (typeof roles)[number];
```

`[number]` reads the element type of the const array, so the union follows the runtime list.

## Common mistakes

- Writing `User[key]` with a runtime variable and getting a 'used as a type' error.

## Key points

- The index is a type, e.g. `User['id']`, or `typeof key` when the key is a variable.
- Array element types use `T[number]`, e.g. `(typeof roles)[number]` for a const array.
- The property type is derived from the source type instead of being copied by hand.

## Interview questions

### How do I get a union of all property value types?

Index with `keyof`: `T[keyof T]` gives the union of every property type.

### Can I index nested properties?

Yes, chain the lookups: `User['address']['city']`.

## Related cards

- [keyof](https://wonderium.dev/ts/keyof.md)
- [typeof](https://wonderium.dev/ts/typeof.md)
- [Mapped Types](https://wonderium.dev/ts/mapped-types.md)
- [as const](https://wonderium.dev/ts/as-const.md)
- [Tuple](https://wonderium.dev/ts/tuple.md)

---

- Level: [TypeScript](https://wonderium.dev/ts.md)
- HTML version: https://wonderium.dev/ts/indexed-access-types
- Last updated: 2026-11-25
