镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

@pelotom

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;

Activity

  1. mhegazy commented on Jun 14, 2018

    @mhegazy
    Contributor

    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.

  2. pelotom commented on Jun 14, 2018

    @pelotom
    Author

    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.

  3. mhegazy commented on Jun 14, 2018

    @mhegazy
    Contributor

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

  4. pelotom commented on Jun 14, 2018

    @pelotom
    Author

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

  5. mhegazy commented on Jun 14, 2018

    @mhegazy
    Contributor

    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

  6. pelotom commented on Jun 14, 2018

    @pelotom
    Author

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

  7. nalply commented on May 13, 2019

    @nalply

    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[]?

  8. monfera commented on Jan 4, 2021

    @monfera

    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 🎉

  9. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions