TypeScript 中的 ES7 Object.entries() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39741428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ES7 Object.entries() in TypeScript not working
提问by Jan Dole?al
I have an issue with transpiling ES7 code with TypeScript. This code:
我在使用 TypeScript 转译 ES7 代码时遇到问题。这段代码:
const sizeByColor = {
red: 100,
green: 500,
};
for ( const [ color, size ] of Object.entries(sizeByColor) ) {
console.log(color);
console.log(size);
}
gives the error:
给出错误:
TypeError: Object.entries is not a function
TypeError: Object.entries is not a function
TypeScript v2.0.3
打字稿 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"
]
}
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.
我想用 迭代槽对象Object.entries()
,所以我分配了内部定义"lib": [ "es2017" ]
,但仍然,打字稿不允许我转译它。
采纳答案by Jan Dole?al
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
嗯,看起来我忘记将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
EDIT:Yey, i tried to modify WebStorm settings, and after set "Use TypeScript service (experimental)" to true, everything is ok !
编辑:是的,我尝试修改 WebStorm 设置,在将“使用 TypeScript 服务(实验性)”设置为 true 后,一切正常!
回答by Oleksandr Nechai
I can reproduce your problem when I have a global compiler but not local one in the ./node_modules.
当我在 ./node_modules 中有全局编译器但没有本地编译器时,我可以重现您的问题。
In my case compiler just does not know which tsconfig.json file to use. Pointing it to particular tsconfig.json file helps:
在我的情况下,编译器只是不知道要使用哪个 tsconfig.json 文件。将其指向特定的 tsconfig.json 文件有助于:
tsc --project ./tsconfig.json
tsc --project ./tsconfig.json
I have also added dom option to the lib, because es2017 does not recognize console:
我还在lib中添加了dom选项,因为es2017无法识别控制台:
"lib": [
"es2017",
"dom"
]
回答by Benny Neugebauer
If you don't want to include the full set of ECMAScript 2017 (ES8)in your set of libraries, then you can also use es2017.object
to just satisfy the resolution of Object.entries
and related.
如果你不想在你的库集中包含完整的ECMAScript 2017 (ES8)集,那么你也可以使用es2017.object
来满足Object.entries
和相关的分辨率。
Here is a minimal working example:
这是一个最小的工作示例:
src/index.ts
源代码/索引.ts
const sizeByColor = {
red: 100,
green: 500,
};
for (const [color, size] of Object.entries(sizeByColor)) {
console.log(color);
console.log(size);
}
tsconfig.json
配置文件
{
"compilerOptions": {
"lib": ["dom", "es2016", "es2017.object"],
"rootDir": "src",
"target": "es6",
"outDir": "dist"
},
"exclude": [
"node_modules"
]
}
Note:If "target"
is set to "es6"
, then TypeScript uses by default the following "lib"
entries (if none are specified): ["dom", "dom.iterable", "es6", "scripthost"]
.
注意:如果"target"
设置为"es6"
,则 TypeScript 默认使用以下"lib"
条目(如果未指定):["dom", "dom.iterable", "es6", "scripthost"]
.
The library defaults get overwritten when setting "lib"
manually, that's why I needed to add "dom"
(to use console.log
in my code) and "es6"
to demonstrate the usage of ES6 and partial ES8 ("es2017.object"
).
"lib"
手动设置时库默认值会被覆盖,这就是为什么我需要添加"dom"
(console.log
在我的代码中使用)并"es6"
演示 ES6 和部分 ES8 ( "es2017.object"
)的用法。
Source: TypeScript Compiler Options
回答by Mateo Tibaquira
For a standalone Typescript app, I had to add the lib es2019.object
to the tsconfig, and import the polyfill to make it work :)
对于独立的 Typescript 应用程序,我必须将 lib 添加es2019.object
到 tsconfig,并导入 polyfill 以使其工作:)
import 'core-js/es/object/from-entries';