You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Object.values and Object.entries return type any when passing an object defined as having number keys #26010
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 of Object.values() and Object.entries() when passing it a type defined as having number keys.
interfaceObjectConstructor{/** * Returns an array of values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */values<T>(o: {[s: string]: T}|ArrayLike<T>): T[];// ...}
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 of Object.values().
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.
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...
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:
enumE{A,B}constfakeEnum={0: 'A',1: 'B',A: 0,B: 1,};constv1=Object.values(E);// v1 has type any[]constv2=Object.values(fakeEnum);// v2 has type (string|number)[]
I just encountered this any inference when migrating some types that were used with Object.entries, resulting in returning [string, any][].
We aim to have no any in our codebase, as it leads to avoidable runtime errors. If there's no resolution to this, we'll likely enforce usage of a wrapper utility to provide better typings.
Would love to see progress on this.
Thank you to the TypeScript team for all your hard work! <3
Reacted by Mikael Lirbank, Joe Bowbeer, Dan Vanderkam, Maksim Nesterenko, Frederik Krautwald, Max Donchenko and David
Activity
mhegazy commentedon Jul 27, 2018
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.
philraj commentedon Jul 30, 2018
What is the reason for the wrong type inference on enums when the type signature allows numeric indexes?
[edit] Removed invalid code example.
philraj commentedon Jul 31, 2018
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:
quicksnap commentedon Feb 21, 2019
I just encountered this
anyinference when migrating some types that were used withObject.entries, resulting in returning[string, any][].We aim to have no
anyin our codebase, as it leads to avoidable runtime errors. If there's no resolution to this, we'll likely enforce usage of a wrapper utility to provide better typings.Would love to see progress on this.
Thank you to the TypeScript team for all your hard work! <3