Repository navigation
New definition of PropertyKey does not observe the keyofStringsOnly setting #24945
Description
Activity
PropertyKey type alias has nothing to do with
keyofbehaviour.--keyofStringsOnlycontrols the behavior ofkeyofto choose whether to use TypeScript 2.9 or pre-TypeScript 2.9 behaviour.For instance,
Object.hasOwnPropertyaccepts a symbol as its input regardless of--keyofStringsOnly.Reacted by ExE Boss- addedWorking as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
on Jun 14, 2018 Ok, but
PropertyKey’s definition was changed in 2.9, coincidentally with thekeyofchange, and it is a breaking change.--keyofStringsOnlyis supposed to prevent breaking changes in 2.9, so it seems like it should cover this as well.--keyofStringsOnly is supposed to prevent breaking changes in 2.9,
it is not a catch all flag. it is meant for a specific use case.
So
PropertyKey's definition was just coincidentally changed tostring | number | symbolat the same time as thekeyofchange? Why?I am not sure i follow.
PropertyKeywas just moved fromlib.es6.d.tstolib.d.tsto consolidate definitions ofObjectmethods. see 68ce69a#diff-a6b488d9bd802977827b535a3011c1f3I see. I'm not sure why this is suddenly breaking my code in 2.9 then, but oh well 🤷♂️. Thanks for the info.
Let me add to this issue and point out something I just discovered today.
The definition of
PropertyKeyis a bit too loose for some definitions inReflect,Proxyand probably a few more. Numbers will never be encountered in these places. This is not a bug but some surprising hidden JavaScript weirdness: keys are coerced to string even for arrays.Example:
Reflect.ownKeys([42])gives[ '0', 'length' ].Perhaps TypeScript should introduce a second, more strict definition like this:
type PropertyKeyOut = string | symbol?So that the return type of
Reflect.ownKeys()inlib.es2015.reflect.d.tscan be defined asPropertyKeyOut[]?Reacted by Robert Monfera and ExE BossAs luck would have it, I almost filed a TS issue today (ES2015 explicitly says that
Reflect.ownKeyscan only ever return an array ofstringandsymbolelements) but one existed already and the fix got merged a few hours ago 🎉Reacted by Daniel Ly and ExE Boss- locked as resolved and limited conversation to collaborators
on Oct 21, 2025
TypeScript Version: 2.9.2
Search Terms:
PropertyKeykeyofStringsOnlyCode
In
lib.es5.d.ts, we have nowhowever this seems incorrect if
"keyofStringsOnly": true. So as to observe that setting, it seems like it should be