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":
tsTrytypePoint = {x :number;y :number };typeP =keyofPoint ;
If the type has astring ornumber index signature,keyof will return those types instead:
tsTrytypeArrayish = { [n :number]:unknown };typeA =keyofArrayish ;typeMapish = { [k :string]:boolean };typeM =keyofMapish ;
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:
OT
RM
MM
S
Last updated: Dec 16, 2025