Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript error:Object.fromEntries typescript Error

I have a function in typescript which uses Object.fromEntries to reduce a complex response object and group it using substring of the child object key.

let Newresult = res.map(object => Object.fromEntries(Object.entries(object).map(([key, value]) => [
key,
value.map(valueobject => Object.entries(valueobject).reduce((res1, [name, value]) => {
    const key = name.slice(0, 5);
    res1[key] = res1[key] || {};
    res[key][name] = value;
    return res1;
}, {}))
])));

but the problem is typescript throws below error while compiling.

error TS2339: Property 'fromEntries' does not exist on type 'ObjectConstructor'.
error TS2339: Property 'map' does not exist on type '{}'.

I tried adding ESNext,ES2017.Object into my tsconfig.json lib-array but still its throwing compilation error. but the same updates for my lib-array is allowing me to use Object.entries.

I am using angular -v-6, typescript:~3.1.1

which other method can help me achieve the same result as above. can somebody direct me in right direction?

Thanks in advance!!

like image 324
user3331232 Avatar asked Jun 04 '20 18:06

user3331232


2 Answers

  • TypeScript version is 3.9

Open tsconfig.json

Add

"target": "esnext",
"lib": ["esnext", "dom"]
like image 136
finalreturn Avatar answered Sep 22 '22 05:09

finalreturn


Object.fromEntries was ES2019, so your lib option needs to include that (or ES2020). (ESNext is a moving target.)

That said, TypeScript v3.1.1 is fairly old. You may need to upgrade.

like image 37
T.J. Crowder Avatar answered Sep 21 '22 05:09

T.J. Crowder