keyof, typeof, and indexed access

The keyof and typeof type operators and indexed access types, including union versus intersection keys and the difference from JavaScript typeof.

テーマページの本文は現在英語です。

keyof T produces a union of the public property names of T. T[K] is an indexed access type: the type of the property or properties named by K. These operators have been in the language since TypeScript 2.1. They work on types, not on runtime values.

typeof in a type position is a type query: it takes the type of a value in scope. It is not the JavaScript typeof operator, which returns a string at runtime. Parenthesize keyof of an intersection or a union. Without parentheses, keyof A & B is (keyof A) & B, which is usually not what you want.

Examples were checked with TypeScript 5.9.3 using tsc --strict. See the TypeScript handbook for the complete utility-type catalog.

TypeScript reference overview →

API index

APISummary
keyof and indexed accessTake the keys of a type, or the type of one or more properties. keyof on a union keeps only shared keys.
typeof (type query)Take the type of a value in scope. This is not the JavaScript typeof operator that returns a string.

keyof and indexed access

keyof Point is "x" | "y". Point["x"] is number, and Point["x" | "y"] is also number because both properties have that type. A missing key such as Point["z"] is a type error. For an optional property, the accessed type includes undefined, as with a?: string under the default --strict options used here (exactOptionalPropertyTypes is not enabled in the project tsconfig).

keyof of a union is the intersection of the keys: only keys present in every member remain. keyof of an intersection is the union of the keys. Always write those with parentheses.

keyof and indexed access

type Key = keyof Point;
type Value = Point["x"];
type Either = Point["x" | "y"];
type Shared = keyof (A | B);
type All = keyof (A & B);

Take the keys of a type, or the type of one or more properties. keyof on a union keeps only shared keys.

Type parameterDescription
TThe object (or array or tuple) type to read keys or properties from.
KA key, a union of keys, number for an array element, or a tuple index. A key that is not in T is a type error.

Produces: A union of keys, or the type of the selected properties. For an optional property, the result includes undefined under default --strict.

Type errors

  • type error — Indexing with a key that does not exist on the type is a type error. Assigning a key that is not in keyof T is also a type error.
  • These examples do not rewrite Record, which is a mapped type over a key union and stays on the Record page.
typescript
type Point = { x: number; y: number };
type Key = keyof Point;
const key: Key = "x";
const value: Point["x"] = 1;
const either: Point["x" | "y"] = 2;
// @ts-expect-error "z" is not a key of Point
const missing: Point["z"] = 1;
// @ts-expect-error "z" is not in keyof Point
const badKey: Key = "z";

type Optional = { a?: string };
const maybe: Optional["a"] = undefined;

type A = { a: string; c: number };
type B = { b: boolean; c: number };
const shared: keyof (A | B) = "c";
const all: keyof (A & B) = "a";
// @ts-expect-error "a" is not in every member of the union
const notShared: keyof (A | B) = "a";

const item: string[][number] = "ok";
console.log(key, shared, item);

Expected output

x c ok

Related: Record, typeof

Official documentation →

typeof in a type position

typeof palette, used as a type, is the type of that value. keyof typeof palette then produces the union of its keys, which is a common way to name the keys of a const object. The same tokens at runtime, typeof n, evaluate to the string "number". Those two meanings of typeof are not interchangeable: a type query is not the string "number". typeof in a type position also cannot query a type alias; it needs a value.

typeof (type query)

type Name = keyof typeof palette;
type Red = (typeof palette)["red"];
type T = typeof n;

Take the type of a value in scope. This is not the JavaScript typeof operator that returns a string.

Type parameterDescription
valueA value in scope, such as a const object or a variable. A type alias is not a valid typeof operand in a type position.

Produces: The type of that value, including literal types when the value was declared with as const.

Type errors

  • type error — Using typeof on a type alias is an error: the alias only refers to a type. Assigning the string "number" to typeof n is also an error when n is a number.
  • The examples were checked with TypeScript 5.9.3 under tsc --strict.
typescript
const palette = { red: "#f00", blue: "#00f" } as const;
type Name = keyof typeof palette;
type Red = (typeof palette)["red"];

function paint(name: Name): (typeof palette)[Name] {
  return palette[name];
}
console.log(paint("red"));
// @ts-expect-error "green" is not a key of palette
paint("green");

const n = 1;
type T = typeof n;
const kind = typeof n;
console.log(kind);
const literal: T = 1;
// @ts-expect-error typeof n in the type position is number, not the string "number"
const wrong: T = "number";

type Point = { x: number };
// @ts-expect-error typeof in a type position queries a value, not a type alias
type Bad = typeof Point;

Expected output

#f00
number

Related: keyof and indexed access

Official documentation →