TypeScript 中的 ES7 Object.entries() 不起作用 [英] ES7 Object.entries() in TypeScript not working

查看:27
本文介绍了TypeScript 中的 ES7 Object.entries() 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 TypeScript 转译 ES7 代码时遇到问题.这段代码:

I have an issue with transpiling ES7 code with TypeScript. This code:

const sizeByColor = {
    red: 100,
    green: 500,
};

for ( const [ color, size ] of Object.entries(sizeByColor) ) {
    console.log(color);
    console.log(size);
}

给出错误:

TypeError: Object.entries 不是函数

TypeScript v2.0.3

tsconfig.json:

tsconfig.json:

{
"compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": true,
    "noEmitOnError": true,
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "pretty": true,
    "lib": [ "es2017" ],
},
"exclude": [
    "node_modules"
],
"include": [
    "./node_modules/@types/**/*.d.ts",
    "./src/**/*.ts"
]
}

我想用 Object.entries() 迭代槽对象,所以我分配了内部定义 "lib": [ "es2017" ],但仍然,打字稿不会允许我转译它.

I want to iterate trough object with Object.entries(), so I assigned internal definitions "lib": [ "es2017" ], but still, typescript wont allow me to transpile it.

推荐答案

嗯,看起来我忘记将 core-js polyfill 注入 Object.entries.import 'core-js/fn/object/entries'; 有了这个 polyfill 转译工作,但 IDE 仍然抱怨它.当我直接包含 @types/core-js 时,IDE 没问题,但是由于lib/es2017"中的重复声明,Typescript 会崩溃.. 看起来 IDE (WebStorm) 无法处理lib"tsconfig.json

Hm, it looks i forgot to inject core-js polyfill to Object.entries. import 'core-js/fn/object/entries'; With this polyfill transpilation works, but IDE is still complaining about it. When I include @types/core-js directly, IDE is ok, but Typescript will crash due to duplicate declarations in "lib/es2017".. It looks like IDE (WebStorm) cant handle "lib" settings inside tsconfig.json

是的,我尝试修改 WebStorm 设置,在将使用 TypeScript 服务(实验性)"设置为 true 后,一切正常!

Yey, i tried to modify WebStorm settings, and after set "Use TypeScript service (experimental)" to true, everything is ok !

这篇关于TypeScript 中的 ES7 Object.entries() 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆