-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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.entries and Object.values are not type safe. #44586
Comments
Same for Object.keys when the object has known keys (ie, Object.keys({ one: 1, two: 2, three: 3 }), you'd expect this to be |
This has been covered before.
|
See #38520. |
Awesome, thanks! Closing this. |
lib Update Request
Configuration Check
My compilation target is
ESNext
and my lib isthe default
.Missing / Incorrect Definition
The generics on
Object.values
andObject.entries
for the value types are unsafe. They narrow the types for the value when it is unsafe to do so. Because any object is assignable to a given object type only if it contains the expected fields,Object.entries
andObject.values
might produce unexpected values, resulting in a runtime error that could've been prevented.The definition for
Object.entries
is slightly confusing: it contains the valid type for the keys (string | number
), which prevents type errors when trying to access properties on the object. However, this is not the case for the value type, which seems inconsistent.The type safe definition would be to set the value type to
unknown
: this forces the user to explicitly check the type of the value before accessing it, while also making it clear that the value type is truly unknown. The current typing is likely to have no runtime errors the majority of the time, while anunknown
typing ensures there are no type errors.Sample Code
Example code:
Documentation Link
None that I'm aware of.
The text was updated successfully, but these errors were encountered: