fromEntries
lib: es2019
should add Object.fromEntries()
type.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries
My suggestion meets these guidelines:
HI Guys,
Is this live yet and if so, which version? I can't get it to find it as Property 'fromEntries' does not exist on type 'ObjectConstructor'.
What versions of TS and NODEJS should I be running to make this work?
The file is available in my node_modules under lib.es2019.object.d.ts
but it won't read from it?
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;
}
HI Guys,
Is this live yet and if so, which version? I can't get it to find it as
Property 'fromEntries' does not exist on type 'ObjectConstructor'.
What versions of TS and NODEJS should I be running to make this work?
It looks to me that in tsconfig.json
, the setting to use is target: "es2019"
or greater. This means it won't work in all browsers and typescrcipt does no magic to use this.
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:
{
foo: 'bar',
baz: 'boo',
alpha: 'beta',
}
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 Object.entries
returns [string, T]
and not a keyof type since you know the first element in each of the array elements is a keyof something and not just a string.
@caseyhoward , Hi. I have a problem with the
Cannot use JSX unless the '--jsx' flag is provided.(17004)
'T' only refers to a type, but is being used as a value here.(2693)
JSX element 'T' has no corresponding closing tag.(17008)
Do you know the solution?
Go into tsconfig.json
, and inside compilerOptions
properties search for a property named lib
. Add the string "esnext" in the array. It should look something like:
"lib": ["es2018", "dom", "esnext"]
Most helpful comment
Go into
tsconfig.json
, and insidecompilerOptions
properties search for a property namedlib
. Add the string "esnext" in the array. It should look something like:"lib": ["es2018", "dom", "esnext"]