[英]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;
}, {}))
])));
但问题是 typescript 在编译时抛出以下错误。
error TS2339: Property 'fromEntries' does not exist on type 'ObjectConstructor'.
error TS2339: Property 'map' does not exist on type '{}'.
我尝试将 ESNext,ES2017.Object 添加到我的 tsconfig.json lib-array 中,但仍然抛出编译错误。 但我的 lib-array 的相同更新允许我使用 Object.entries。
我正在使用 angular -v-6, typescript:~3.1.1
哪种其他方法可以帮助我达到与上述相同的结果。 有人可以指导我正确的方向吗?
提前致谢!!
Object.fromEntries
是ES2019 ,因此您的lib
选项需要包含它(或 ES2020)。 (ESNext 是一个移动的目标。)
也就是说,TypeScript v3.1.1 已经相当老了。 您可能需要升级。
打开tsconfig.json
添加
"target": "esnext",
"lib": ["esnext", "dom"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.