Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New definition of PropertyKey does not observe the keyofStringsOnly setting #24945

Closed
pelotom opened this issue Jun 14, 2018 · 8 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@pelotom
Copy link

pelotom commented Jun 14, 2018

TypeScript Version: 2.9.2

Search Terms: PropertyKey keyofStringsOnly

Code

In lib.es5.d.ts, we have now

declare type PropertyKey = string | number | symbol;

however this seems incorrect if "keyofStringsOnly": true. So as to observe that setting, it seems like it should be

declare type PropertyKey = keyof any;
@mhegazy
Copy link
Contributor

mhegazy commented Jun 14, 2018

PropertyKey type alias has nothing to do with keyof behaviour. --keyofStringsOnly controls the behavior of keyof to choose whether to use TypeScript 2.9 or pre-TypeScript 2.9 behaviour.

For instance, Object.hasOwnProperty accepts a symbol as its input regardless of --keyofStringsOnly.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 14, 2018
@pelotom
Copy link
Author

pelotom commented Jun 14, 2018

Ok, but PropertyKey’s definition was changed in 2.9, coincidentally with the keyof change, and it is a breaking change. --keyofStringsOnly is supposed to prevent breaking changes in 2.9, so it seems like it should cover this as well.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 14, 2018

--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.

@pelotom
Copy link
Author

pelotom commented Jun 14, 2018

So PropertyKey's definition was just coincidentally changed to string | number | symbol at the same time as the keyof change? Why?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 14, 2018

I am not sure i follow. PropertyKey was just moved from lib.es6.d.ts to lib.d.ts to consolidate definitions of Object methods. see 68ce69a#diff-a6b488d9bd802977827b535a3011c1f3

@pelotom
Copy link
Author

pelotom commented Jun 14, 2018

I see. I'm not sure why this is suddenly breaking my code in 2.9 then, but oh well 🤷‍♂️. Thanks for the info.

@pelotom pelotom closed this as completed Jun 14, 2018
@nalply
Copy link

nalply commented May 13, 2019

Let me add to this issue and point out something I just discovered today.

The definition of PropertyKey is a bit too loose for some definitions in Reflect, Proxy and 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() in lib.es2015.reflect.d.ts can be defined as PropertyKeyOut[]?

@monfera
Copy link

monfera commented Jan 4, 2021

As luck would have it, I almost filed a TS issue today (ES2015 explicitly says that Reflect.ownKeys can only ever return an array of string and symbol elements) but one existed already and the fix got merged a few hours ago 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants