Skip to content

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

Merged
merged 1 commit into from
Mar 18, 2020

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Mar 18, 2020

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 be PropertyKey.

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:

declare function fromEntries<K extends PropertyKey, T>(entries: Iterable<readonly [K, T]>): { [k in K]: T };

But all this really does is let you make arraylikes from fromEntries, and I don't think anybody is asking for that.

Fixes #31393

PropertyKey is accurate but not usable.

Fixes #31393
@sandersn
Copy link
Member Author

@rbuckton @weswigham I'd like to get this into 3.9, so can you review it today or tomorrow please?

@sandersn
Copy link
Member Author

@typescript-bot user test this
@typescript-bot run dt

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 18, 2020

Heya @sandersn, I've started to run the parallelized Definitely Typed test suite on this PR at 02b2cde. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 18, 2020

Heya @sandersn, I've started to run the parallelized community code test suite on this PR at 02b2cde. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master.

@sandersn
Copy link
Member Author

Users tests are clean; not much surprise there.

Copy link
Member

@weswigham weswigham left a 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.

@sandersn
Copy link
Member Author

Definitely Typed is also clean.

@sandersn sandersn merged commit 062104d into master Mar 18, 2020
@sandersn sandersn deleted the fix-fromEntries-index-signatures branch March 18, 2020 21:43
@osdiab
Copy link

osdiab commented Mar 20, 2020

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.

@lcswillems
Copy link

Several times I wanted to convert a [number, string] into a Record<number, string>, but Object.fromEntries converts it to Record<string, string>.

Would it be possible to change the type to:

declare function fromEntries<K extends PropertyKey, T>(entries: Iterable<readonly [K, T]>): { [k in K]: T };

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ES2019 Object.fromEntries uses PropertyKeys as mapped key type
5 participants