TypeScript Reference ยท Utility Types
TypeScript PropertyKey type
Reference for PropertyKey in TypeScript: the built-in union string | number | symbol, how it relates to keyof any, Reflect, and generic key parameters.
PropertyKey is a built-in type alias for string | number | symbol: every type that can be used as a property key in JavaScript. It is what keyof of an object with an unknown shape resolves to, and what many standard APIs (Reflect.get, Object.defineProperty, Object.hasOwn) accept as a key.
Use it when you write generic helpers that handle keys without knowing whether they are strings, numbers or symbols, and as a constraint for "any key" type parameters.
type PropertyKey = string | number | symbol;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
declare type PropertyKey = string | number | symbol;
It is declared in the core library (lib.es5.d.ts) and is always available. There are no parameters; the table below lists the three members.
| Member | Description |
|---|---|
string |
Ordinary names, including numeric-looking ones ("1"). |
number |
Array-style indices; at runtime they are converted to strings. |
symbol |
Unique keys such as Symbol.iterator or ones you create with Symbol(). |
Edge cases:
keyof anyisstring | number | symbol, the same set.keyof {}isnever, andkeyof objectisneveras well.- Since TypeScript 2.9,
keyof Tcan includenumberandsymbol, so code that assumed string keys needskeyof T & stringorExtract<keyof T, string>. - The old
keyofStringsOnlycompiler option, which madekeyofreturn onlystring, is deprecated and no longer supported by current compilers. bigintis not a property key; a bigint used as a key is converted to a string at runtime.
Usage notes
- Constrain generic keys with
K extends PropertyKeywhen you build aRecord<K, V>:Record's own key constraint iskeyof any, so the two line up. - Narrow with
typeof key === "symbol"when you need to treat symbols separately, for example beforeString(key)(concatenating a symbol throws). - Object literals with computed keys accept any
PropertyKey, but index signatures only allowstring,number,symboland template-literal patterns, not unions of them. - JavaScript turns numbers into strings, so
obj[1]andobj["1"]read the same slot. - It describes keys, not values. For the keys of one specific type use
keyof T.