-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Simplify return type of Object.fromEntries #37457
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
Conversation
PropertyKey is accurate but not usable. Fixes #31393
@rbuckton @weswigham I'd like to get this into 3.9, so can you review it today or tomorrow please? |
@typescript-bot user test this |
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
Users tests are clean; not much surprise there. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But all this really does is let you make arraylikes from fromEntries, and I don't think anybody is asking for that.
I mean, making strongly typed object from tuples of tuples seems ok, but likely improvement is improvement.
Definitely Typed is also clean. |
I personally like to do what @weswigham suggested - a simple implementation of mapping object values that I've used before is as follows: function mapValues<T, V>(obj: T, mapFn: (v: T[keyof T]) => V): {[k in keyof T]: V} {
return Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, mapFn(v)]));
} But without a type suggestion this doesn't work without the more precise typing. It's not a big deal to add a type suggestion but I wouldn't say it's useless. |
Several times I wanted to convert a Would it be possible to change the type to:
? |
Returning a mapped type over PropertyKey is accurate but not usable.
{ [k in Property]: T }
produces two index signatures:{ [k: string]: T, [k: number]: T }
, but that's not what people expect.This PR simplifies the return type to just
{ [k: string]: T }
; it still allows the input key type to bePropertyKey
.Symbols are still dropped on the floor, just like the rest of the compiler.
An alternative fix would be to infer a type variable from the key type as well:
But all this really does is let you make arraylikes from
fromEntries
, and I don't think anybody is asking for that.Fixes #31393