Add ES2019 Object.fromEntries function#30934
Conversation
| * Returns an object created by key-value entries for properties and methods | ||
| * @param entries An iterable object that contains key-value entries for properties and methods. | ||
| */ | ||
| fromEntries(entries: Iterable<[string | symbol, any]>): any; |
There was a problem hiding this comment.
it would be really ideal if this could produce an object where the values matched the value item in the iterable. Perhaps?:
| fromEntries(entries: Iterable<[string | symbol, any]>): any; | |
| fromEntries<T = unknown>(entries: Iterable<[PropertyKey, T]>): { [k in PropertyKey]: T }; |
There was a problem hiding this comment.
Good suggestion, but a bit hesitant to do unknown here as no other function in lib.d.ts uses it for its return type. Would any be sufficient or unknown be required?
There was a problem hiding this comment.
Since T is inferable then any is fine, but imo nothing should ever be any, only unknown or narrower :-)
There was a problem hiding this comment.
Kinda agree but I'll go any for now, for consistency here...
| * Returns an object created by key-value entries for properties and methods | ||
| * @param entries An iterable object that contains key-value entries for properties and methods. | ||
| */ | ||
| fromEntries<T = any>(entries: Iterable<[PropertyKey, T]>): { [k in PropertyKey]: T }; |
There was a problem hiding this comment.
| fromEntries<T = any>(entries: Iterable<[PropertyKey, T]>): { [k in PropertyKey]: T }; | |
| fromEntries<T = any>(entries: Iterable<readonly [PropertyKey, T]>): { [k in PropertyKey]: T }; |
See #29721
|
@daniel19801 DANIEL PLEASE READ! - You cannot turn off GitHub email notifications by replying with "stop" or some variant thereof; all this is doing is posting comments to GitHub threads. Please go to your GitHub profile and turn off email notifications. |
|
@saschanaz it'd also be great if you wanted to create a DT package for https://www.npmjs.com/package/object.fromentries using these types, once this PR is merged :-) |
|
Apologies if this is the wrong place for this but I was playing with this type locally with the package. (Which has really helped me out, so massive thanks 🎉 If I have something of the form This goes away if I make a second type parameter for fromEntries which extends |
|
@AWare That suggestion looks good, you can open a new issue so that the project maintainers can start thinking about it. |
|
If anyone is looking for something with more generic hacking, type PairToObject<P extends readonly [PropertyKey, any]> = (
P extends any ?
{
[k in P[0]] : P[1]
} :
never
);
type ToUnaryFunctionUnion<U> = (
U extends any ?
(arg : U) => void :
never
);
type UnionToIntersection<U> = (
ToUnaryFunctionUnion<U> extends (arg : infer I) => void ?
I :
never
);
type _<T> = T;
type Merge<T> = _<{ [k in keyof T] : T[k] }>;
declare function fromEntries<
P extends readonly [PropertyKey, any]
> (i : Iterable<P>) : (
/**
* The `Merge<>` part isn't necessary.
*/
Merge<
UnionToIntersection<
PairToObject<P>
>
>
);
declare const entries : [
["a", 1],
["b", 2]
];
/*
OK!
{
a: 1;
b: 2;
}
*/
const x = fromEntries(entries);
/*
OK!
{
a: 1;
b: 2;
}
*/
const y = fromEntries([
["a", 1],
["b", 2]
] as const);
/*
NOT WHAT WE EXPECTED,
{
[x: string]: number;
}
So, the `as const` "hack" is needed
*/
const z = fromEntries([
["a", 1],
["b", 2]
]); |
Fixes #30933, fixes #25999
We already have #26149 but it hasn't been active nor reached a good consensus, so this PR adds a simpler type with potential future progressive enhancement with generics.