# Indexed Access Types

> Extracting types from specific object properties.

Subject: TypeScript | Lesson 27 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/indexed-access-types
Previous: [Const Type Parameters](https://codewithanimation.in/typescript/const-type-parameters.md) | Next: [Conditional Types](https://codewithanimation.in/typescript/conditional-types.md)

## Object property lookup, but for types

In JavaScript, you access an object's property using bracket notation: `user['address']`.

In TypeScript, you can use the exact same bracket notation to look up a *type* on another type. This is called an Indexed Access Type.

## Don't Repeat Yourself (DRY)

Imagine an API returns a massive `Product` type, but your UI component only needs the deeply nested `reviews` array. You shouldn't manually recreate the `Review` type from scratch.

Instead, you dynamically extract it: `type ReviewList = Product['reviews']`. If the backend changes the API, your extracted type updates automatically!

## Syntax

```typescript
type User = {
  id: number;
  name: string;
  address: {
    street: string;
    city: string;
  };
};

// 1. Single extraction
type ID = User["id"]; // number

// 2. Nested extraction
type City = User["address"]["city"]; // string

// 3. Union extraction (Extracting multiple properties)
type IdOrName = User["id" | "name"]; // number | string
```

## Common mistake: Types only, no runtime values

A common mistake is trying to pass a JavaScript variable into the brackets.

```typescript
const key = "name";
type T = User[key]; // Error!
```

You can only pass *types* into the brackets. To fix this, you must use a literal type or `typeof`: `type T = User[typeof key]`.

## Tip: Extracting Array Elements

You can extract the type of an array's elements by passing `number` as the index: `type Element = string[]['number']` (resolves to `string`).

## Try it: Try it

Select different property paths to see how the TypeScript compiler reaches deep into the `User` object and extracts the exact underlying type.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/indexed-access-types

## Check yourself

### Question 1

Given `type Settings = { theme: 'dark' | 'light', notifications: boolean }`, what does `type T = Settings['theme' | 'notifications']` resolve to?

- A. 'dark' | 'light' | boolean
- B. { theme: 'dark' | 'light', notifications: boolean }
- C. Error: You can only pass one string at a time.

Correct answer: A

Passing a union of keys returns a union of the corresponding property types.

- Why not B: Indexed access extracts the specific property types, it doesn't return the whole object.
- Why not C: TypeScript fully supports passing union types to extract multiple properties.

## Remember this

- Use bracket notation `Type['key']` to extract a specific property's type.
- You can chain brackets for deep extraction: `Type['a']['b']`.
- You can pass a union of keys `Type['a' | 'b']` to get a union of the resulting types.
- You must pass a *type* into the brackets, not a runtime variable.
