我在typescript中有一个函数,它使用Object.fromEntries来简化一个复杂的响应对象,并使用子对象键的子字符串对其进行分组。
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 '{}'.我尝试在我的tsconfig.json库数组中添加ES2017对象,但仍然抛出编译错误。但是我的lib-array的相同更新允许我使用Object.entries。
我正在使用angular -v-6,typescript:~3.1.1
还有哪种方法可以帮助我达到与上述相同的结果。有人能告诉我正确的方向吗?
提前感谢!!
发布于 2020-06-05 02:32:23
Object.fromEntries是ES2019,所以您的lib选项需要包括它(或ES2020)。(ESNext是一个移动的目标。)
也就是说,TypeScript v3.1.1是相当旧的。您可能需要升级。
发布于 2020-06-13 18:15:57
打开tsconfig.json
添加
"target": "esnext",
"lib": ["esnext", "dom"]https://stackoverflow.com/questions/62201724
复制相似问题