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
Object.values and Object.entries return type any when passing an object defined as having number keys #26010
Comments
It was originally defined to allow numeric index signatures, but that broke enums (#21089 as you have referenced earlier). I do not think there is a good solution that does not break something. |
What is the reason for the wrong type inference on enums when the type signature allows numeric indexes? [edit] Removed invalid code example. |
In #21089 you wrote:
Is this too dangerous to attempt? Shouldn't enums have that by default, since numeric enums have the property of reverse mappings? I don't understand the following discrepancy: enum E { A, B }
const fakeEnum = {
0: 'A',
1: 'B',
A: 0,
B: 1,
};
const v1 = Object.values(E); // v1 has type any[]
const v2 = Object.values(fakeEnum); // v2 has type (string|number)[] |
I just encountered this We aim to have no Would love to see progress on this. Thank you to the TypeScript team for all your hard work! <3 |
TypeScript Version: 3.1.0-dev.20180727
Search Terms: Object.values Object.entries wrong type inference any
Code
Expected behavior: Type of
v
should be(string | null)[]
.Actual behavior: Type of
v
isany[]
.Playground Link: Typescript Playground doesn't support
Object.values()
.Related Issues: #21089
This bug seems to occur due to the type definitions for ObjectConstructor, which used to include the possibility of number keys in their type signatures. I'm not sure exactly why this was changed to
ArrayLike
to resolve the related issue I linked (this is the exact PR), but it breaks the type inference on the results ofObject.values()
andObject.entries()
when passing it a type defined as havingnumber
keys.This type signature used to be
values<T>(o: { [s: string]: T } | { [n: number]: T }): T[]
, and with this older signature, the example code properly types the result ofObject.values()
.The text was updated successfully, but these errors were encountered: