Skip to the workspace
Pebellslearn by building
Combining Types  ›  Capstone Project

The keyof Operator

keyof Operator 12 minkeyof T produces a type representing the property keys of T. Those keys may include string, number and symbol types.

typescript / The keyof Operator

Explore each scene at your own pace.

01 · The rule

The rule

The keyof Operator

keyof T produces a type representing the property keys of T. Those keys may include string, number and symbol types.

Derive valid property names from a type to constrain a lookup operation.

The tracker lets users sort tasks by any column: id, title or done. A sortBy(key: string) function accepts "titel" just as happily as "title", and the bug only appears when the list refuses to sort. keyof lets the compiler know exactly which keys exist.

The rule

keyof T is the union of T's property names.

type Task = { id: number; title: string; done: boolean };

type TaskKey = keyof Task;   // "id" | "title" | "done"

let k: TaskKey = "title";    // fine
k = "titel";                 // error: not a key of Task

keyof is a type operator: it takes an object type and produces a union of string literal types, one per property. Add a property to Task and TaskKey updates itself.

Its natural partner is the indexed access type T[K], which looks up the type of a property:

type TitleType = Task["title"];        // string
type AnyValue = Task[keyof Task];      // number | string | boolean

Put them together with a generic function and you get a property reader that is safe for every key:

function getField<K extends keyof Task>(task: Task, key: K): Task[K] {
  return task[key];
}

const t: Task = { id: 1, title: "Buy milk", done: false };
getField(t, "done");    // type: boolean
getField(t, "title");   // type: string
getField(t, "owner");   // error: "owner" is not a key of Task

K extends keyof Task means "K must be one of Task's keys". The return type Task[K] then follows whichever key was passed. Do not worry if generics are new — here the pattern is all you need.

keyof also pairs with typeof, which turns a value into its type:

const labels = { todo: "To do", done: "Done" };
type LabelKey = keyof typeof labels;   // "todo" | "done"
Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.