Was this page helpful?

Keyof Type Operator

Thekeyof type operator

Thekeyof operator takes an object type and produces a string or numeric literal union of its keys.The following typeP is the same type astype P = "x" | "y":

ts
typePoint = {x:number;y:number };
typeP =keyofPoint;
type P = keyof Point
Try

If the type has astring ornumber index signature,keyof will return those types instead:

ts
typeArrayish = { [n:number]:unknown };
typeA =keyofArrayish;
type A = number
 
typeMapish = { [k:string]:boolean };
typeM =keyofMapish;
type M = string | number
Try

Note that in this example,M isstring | number — this is because JavaScript object keys are always coerced to a string, soobj[0] is always the same asobj["0"].

keyof types become especially useful when combined with mapped types, which we’ll learn more about later.

The TypeScript docs are an open source project. Help us improve these pagesby sending a Pull Request

Contributors to this page:
OTOrta Therox  (3)
RMRoman Mahotskyi  (1)
MMMasashi Miyazaki  (1)
Ssuica  (1)

Last updated: Dec 16, 2025