Codectionary / Developer documentation / TypeScript

Looking Up Property Types

Indexed access types retrieve a property’s type from another type. Indexing an array type with number retrieves its element type. These operators keep related declarations aligned when the original model changes; the index in a type position must itself be a type.

Syntax

type Name = User["name"];
type Row = Rows[number];

Examples

Looking Up Property Types: working example

Run this standalone example after compiling with TypeScript 5.4+ in strict mode, targeting ES2022. Expected result: The program prints 2 × Book.

type Order = { id: string; lines: { label: string; quantity: number }[] };
type Line = Order["lines"][number];
function summarize(line: Line): string {
  return `${line.quantity} × ${line.label}`;
}
console.log(summarize({ label: "Book", quantity: 2 }));
  1. Expected result: The program prints 2 × Book.
  2. Order["lines"] obtains the array type.
  3. The second lookup [number] obtains one line’s object shape without redeclaring it.

Best practices

  • Derive component and helper parameter types from the authoritative model.
  • Use typeof when starting from a value instead of an existing type alias.

Runtime & versions

Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

TypeScript Handbook

Specifications & further reading