Skip to main content

Lesson 3 of 8

Indexed Access Types - `T[K]`

Indexed access types let you look up the type of a specific property in another type using bracket notation. Just as JavaScript lets you access `obj['key']` at runtime, TypeScript lets you access `T['key']` at compile time.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Indexed Access Types - Deriving Types from Types

Indexed Access Types - Deriving Types from Types
// ── Basic indexed access ─────────────────────────────────────
interface Task {
  id: number;
  title: string;
  status: "todo" | "in-progress" | "done" | "archived";
  assignee: { id: number; name: string; email: string } | null;
  tags: readonly string[];
}

type TaskId     = Task["id"];       // number
type TaskStatus = Task["status"];   // "todo" | "in-progress" | "done" | "archived"
type Assignee   = Task["assignee"]; // { id: number; name: string; email: string } | null

// ── Union access - get types for multiple keys ────────────────
type TaskStringFields = Task["title" | "status"];
// string | "todo" | "in-progress" | "done" | "archived"
// (unions the types of both fields)

// ── keyof union - get all value types ─────────────────────────
type AllTaskValues = Task[keyof Task];
// number | string | ("todo"|"in-progress"|"done"|"archived") | ...

// ── Array element type ────────────────────────────────────────
type TagType  = Task["tags"][number];     // string (element of tags array)
type TagArray = Task["tags"];             // readonly string[]

// ── Nested access ────────────────────────────────────────────
type AssigneeEmail = NonNullable<Task["assignee"]>["email"];  // string

// ── From a real tuple ─────────────────────────────────────────
const PRIORITIES = ["low", "medium", "high", "critical"] as const;
type Priority = (typeof PRIORITIES)[number];
// "low" | "medium" | "high" | "critical"

// ── Practical: safe update function ──────────────────────────
// Set a single typed field on Task:
function setField<K extends keyof Task>(
  task: Task,
  key: K,
  value: Task[K],   // T[K] - must match the field's actual type!
): Task {
  return { ...task, [key]: value, updatedAt: new Date() };
}

setField(task, "status", "done");    // ✅
setField(task, "title", "New");      // ✅
// setField(task, "title", 42);      // ❌ Error: number not assignable to string
// setField(task, "mistyped", "x");  // ❌ Error: not a key of Task

Common Mistakes

  • Using a string literal instead of keyof - `type X = Task['unknown']` causes a TS error because 'unknown' is not a key of Task. This is intentional - it catches typos at compile time.
  • Forgetting `[number]` to unwrap array element types - `Task['tags']` gives `readonly string[]`. Add `[number]` to get the element type: `Task['tags'][number]` = `string`.
  • Overusing indexed access for simple lookups - `type Title = Task['title']` is useful when building generic utilities, but `type Title = string` is clearer for simple cases. Use indexed access when you need the type to update automatically when the interface changes.

Tip

Tip

Practice Indexed Access Types in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Indexed Access Types - `T[K]`

Practice Task

Note

Practice Task - (1) Write a working example of Indexed Access Types from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Indexed Access Types is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready typescript code.

Key Takeaways

  • Indexed access types let you look up the type of a specific property in another type using bracket notation.
  • Using a string literal instead of keyof - `type X = Task['unknown']` causes a TS error because 'unknown' is not a key of Task. This is intentional - it catches typos at compile time.
  • Forgetting `[number]` to unwrap array element types - `Task['tags']` gives `readonly string[]`. Add `[number]` to get the element type: `Task['tags'][number]` = `string`.
  • Overusing indexed access for simple lookups - `type Title = Task['title']` is useful when building generic utilities, but `type Title = string` is clearer for simple cases. Use indexed access when you need the type to update automatically when the interface changes.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done