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

[TS2.5 regression] Object.values(someEnum) returns string[] (not any[]/number[]) #21089

Closed
mprobst opened this issue Jan 9, 2018 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue

Comments

@mprobst
Copy link
Contributor

mprobst commented Jan 9, 2018

TypeScript Version: 2.7.0-dev.201xxxxx

Code

enum X {A, B}

const xs = Object.values(X);
xs.includes(1);

Expected behavior:

In TS2.5, this used to compile, because xs was any[].

Actual behavior:

In TS2.6+, xs is string[]. That's wrong:

> Object.values({A: 1, B: 2})
[ 1, 2 ]

Related:

Curiously, this only happens for enums. {[k: ...}: V} style objects work fine.

const z: {[k: string]: number} = {
  A: 1
}
const zs = Object.values(z);
zs.includes(1);
@mhegazy
Copy link
Contributor

mhegazy commented Jan 9, 2018

The definition of values changed, and that seems to trigger this behaviour...

one option is to give enums a string indexer to string | number in addition to the number indexer, but I am not sure i understand what that would affect...

The other is to change definition of values, and use ArrayLike<T> instead of { [x: number] : T }. this seems like a smaller change.. I will give this a try.

@mhegazy mhegazy added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Jan 9, 2018
@mhegazy mhegazy self-assigned this Jan 9, 2018
@mhegazy mhegazy added this to the TypeScript 2.7 milestone Jan 9, 2018
mhegazy added a commit that referenced this issue Jan 10, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jan 17, 2018
mhegazy added a commit that referenced this issue Jan 17, 2018
…s and Object.entries (#21129)

* Fix #21089: Do not infer from numeric index signature in Object.values and Object.entries

* Update test
@microsoft microsoft locked and limited conversation to collaborators Jul 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

2 participants