“ObjectConstructor”类型中不存在属性“条目” [英] Property 'entries' does not exist on type 'ObjectConstructor'

查看:714
本文介绍了“ObjectConstructor”类型中不存在属性“条目”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ng2实现。我正在使用以下函数调用将对象转换为数组:

I'm working on an ng2 implementation. I'm using the following function call to convert an object to an array:

var authors = Object.entries(responseObject.Authors);

这是一个标准的js函数。但是,ts编译器返回以下错误:

This is a standard js function. However, the ts compiler returns the following error:

"Property 'entries' does not exist on type 'ObjectConstructor'"

基于快速谷歌,似乎解决方案可能是将compilerOptions目标属性从es5更改为es6。但是,在之前的一些问题的研究之后,我认为我能够通过在我的tsconfig.json下面添加额外的lib属性来利用es6功能:

Based on a quick google it appears that the solution may be to change the compilerOptions target property from es5 to es6. However, after some previous research for a previous issue, I thought that I was able to leverage es6 functionality by including the additional "lib" property on my tsconfig.json below:

  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../Scripts/",
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "moduleResolution": "node",
    "lib": [
      "es2015",
      "dom"
    ]
  }

我也尝试将目标属性更改为es2015和然后重建项目并执行typescriptUsingTsConfig,但我仍然得到相同的错误。知道我可以在这里做什么来利用Object.entries()函数吗?

I also tried changing the target property to es2015 and then rebuilt the project and executed "typescriptUsingTsConfig" but I still get the same error. Any idea what I can do here in order to leverage the Object.entries() function?

推荐答案

你说得对更改 target 是错误的方法,更改 lib 是正确的方法,但是您指定了错误的语言版本。 根据MDN Object.entries 在ES2017规范中正式添加。

You're quite correct that changing target is the wrong approach and changing lib is the correct approach, however you have specified the wrong version of the language. According to MDN, Object.entries was officially added in the ES2017 specification.

"lib": ["es2017"]

因此您必须指定*。

如果您希望仅添加 添加了 Object 函数的方法的声明 ES2017,TypeScript允许您指定更精细的价值

If you wish to add only the declarations for the methods of the Object function that were added in ES2017, TypeScript allows you to specify a more granular value.

"lib": ["es2017.object"]

正如Alexander Bird所述,默认情况下,lib的隐含值选项取决于为target指定的值(如果存在)。

As noted by Alexander Bird, by default, the implicit value of the "lib" option is dependent on the value specified for "target" if present.

例如:

"target": "es2017"

将导致相应的前缀lib。*默认情况下包含,除非明确指定lib

Will cause the correspondingly prefixed "lib.*" to be included by default unless "lib" is specified explicitly.

注意你可能希望添加一个实现本身的polyfill,比如这个,以确保在较旧的运行时有效。

Note that you will likely wish to add a polyfill of the implementation itself, such as the this one, to ensure this works in older runtimes.

注意:作为替代方案,您可以指定任何更高版本

Note: as an alternative you can specify any later version

"lib": ["es2018"]

或自然甚至

"lib": ["esnext"]

这最后将包括TypeScript语言已知的最新标准库功能的声明。因为它代表一个移动目标,所以应该小心使用此选项,因为根据定义,所有相应的运行时填充是一项复杂的任务,需要研究,并且可能涉及根据目标运行时加载不同的polyfill。

This last will include the declarations for the very latest standard library features known to the TypeScript language. As it represents a moving target, this option should be used with care since polyfilling all of the corresponding runtime is by definition a complex task that will require research and may involve loading different polyfills depending on your target runtime.

注意:一些评论者问为什么更改 - 目标而不是<$ c $是错误的c> - lib 因为两者都会启用代码来检查?原因是 - target 更改了代码的编译方式。例如,target:es2017表示不会为较旧的运行时转换 async 函数。这是不正确的,因为意图是允许使用其他库,而不是更改输出语法,并且重要的是不要使用库功能完成语法功能。

Note: a few commenters asked why it would be wrong to change --target instead of --lib as both would enable the code to type check? The reason is that --target changes how the code is transpiled. For example, "target": "es2017" means that async functions won't be transformed for older runtimes. It is incorrect because the intent was to enable the use of additional libraries, not to change the output syntax, and it is important not to complete syntactic features with library features.

这篇关于“ObjectConstructor”类型中不存在属性“条目”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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