AI Hints
Home›TypeScript Tutorial›Type Operators
TypeScript · Lesson 62 of 120

Indexed Access Types

A property's type.

⏱ 2 min read▶ Video at 48:14🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Square brackets on a type pick out one property's type. Bike, then price in brackets, is number. It's like reading a property, but for types.

Indexed Access Types
From the video: Indexed Access Types
  • Square brackets
  • One property's type
  • Like reading a property

Picking property types

Order has a nested customer, and a list of lines. Brackets pick the customer type, and the type of one line, using number for array items. Run it.

app.tstypescript
type Order = {
  id: number;
  customer: { name: string; email: string };
  lines: { product: string; qty: number }[];
};

type Customer = Order["customer"];
type Line = Order["lines"][number];

const who: Customer = { name: "Priya", email: "priya@example.com" };
const line: Line = { product: "Helmet", qty: 2 };
console.log(who.name, "|", line.product, "x", line.qty);
Output
Priya | Helmet x 2

Summary

Indexed access types pick a property's type, and number picks an array's item type. Next, mapped types.

Key takeaways
  • Order['customer']
  • T[number]
  • Stay in sync
Next up: Mapped Types — transform every key