-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add ES2019 Object.fromEntries function #30933
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
Comments
HI Guys, Is this live yet and if so, which version? I can't get it to find it as What versions of TS and NODEJS should I be running to make this work? |
The file is available in my node_modules under Any link to any issue regarding how to resolve a similar issue is welcomed. /// <reference no-default-lib="true"/>
/// <reference lib="es2015.iterable" />
interface ObjectConstructor {
/**
* 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<readonly [PropertyKey, T]>): { [k in PropertyKey]: T };
/**
* 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<readonly any[]>): any;
} |
It looks to me that in |
this is what I did declare global {
interface ObjectConstructor {
fromEntries(xs: [string|number|symbol, any][]): object
}
}
const fromEntries = (xs: [string|number|symbol, any][]) =>
Object.fromEntries ? Object.fromEntries(xs) : xs.reduce((acc, [key, value]) => ({...acc, [key]: value}), {}) |
Extending @bennypowers to sort an object similar to this:
use Object.keys(input).sort().reduce((acc, cur) => ({ ...acc, [cur]: input[cur] }), {}) |
I ended up just defining my own functions for what I needed. They're pretty gross since they use any, but they get the job done for me I think: function fromEntries<T>(entries: [keyof T, T[keyof T]][]): T {
return entries.reduce(
(acc, [key, value]) => ({ ...acc, [key]: value }),
<T>{}
);
}
function toEntries<T>(obj: T): [keyof T, T[keyof T]][] {
const entries: [string, any][] = Object.entries(obj);
const entriesWithKeysLookedUp: [keyof T, T[keyof T]][] = entries.map(item => {
const keyString = item[0];
const value = item[1];
return [<keyof T>keyString, value];
});
return entriesWithKeysLookedUp;
} All I wanted to do was map an objects values. Ended up being way more annoying than it should have been. Does anyone know a better way? It's annoying that |
@caseyhoward , Hi. I have a problem with the {} construct. TypeScript 3.8.3. http://www.typescriptlang.org/v2/en/play?useDefineForClassFields=true&allowUnreachableCode=true&allowUnusedLabels=true&downlevelIteration=true&target=99
Do you know the solution? |
Go into
|
Add ["esnext"] to the lib worked for me! Thank u |
Hi All, with the uses of case .ts : case Vendor : what is the right match so that all the dependency can be added in compile ? but adding this to the polyfills make it work for all the cases . ame right ? *platform is web . intention is to support the newer libs for older codebase dependency . Thanks , |
go to "tsconfig.json" and adding :
|
Search Terms
fromEntries
Suggestion
lib: es2019
should addObject.fromEntries()
type.Use Cases
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: