Javascript “ObjectConstructor”类型上不存在属性“条目”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45422573/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:03:23  来源:igfitidea点击:

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

javascriptangulartypescriptecmascript-6es2017

提问by user8334943

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

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

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

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

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

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

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 目标属性从 es5 更改为 es6。但是,在对上一问题进行了一些先前的研究之后,我认为我可以通过在下面的 tsconfig.json 中包含额外的“lib”属性来利用 es6 功能:

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

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?

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

回答by Aluan Haddad

You're quite correct that changing targetis the wrong approach and changing libis the correct approach, however you have specified the wrong version of the language. According to MDN, Object.entrieswas officially added in the ES2017 specification.

您非常正确,更改target是错误的方法,更改lib是正确的方法,但是您指定了错误的语言版本。根据 MDNObject.entries已正式添加到 ES2017 规范中。

"lib": ["es2017"]

is therefore what you must specify instead*.

因此是您必须指定的内容*。

If you wish to add onlythe declarations for the methods of the Objectfunction that were added inES2017, TypeScript allows you to specify a more granular value.

如果您只想添加ES2017Object添加的函数方法的声明,TypeScript 允许您指定更细粒度的 value

"lib": ["es2017.object"]

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

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

For example:

例如:

"target": "es2017"

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

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

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.

请注意,您可能希望添加实现本身的 polyfill,例如 this one,以确保它在较旧的运行时中有效。

Note:as an alternative you can specify any later version

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

"lib": ["es2020"]

or naturally even

甚至自然地

"lib": ["esnext"] 

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.

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

Additionally, the array nature of the "lib"option allows you to combine multiple values to match your runtime. For example, to match es2015 capable web browsers with the addition of these object methods as provided by a polyfill, you can write

此外,该"lib"选项的数组性质允许您组合多个值以匹配您的运行时。例如,要将支持 es2015 的 Web 浏览器与添加这些由 polyfill 提供的对象方法相匹配,您可以编写

"lib": ["es2015", "es2017.object", "dom"]

Note:a few commenters asked why it would be wrong to change --targetinstead of --libas both would enable the code to type check? The reason is that --targetchanges how the code is transpiled. For example, "target": "es2017"means that asyncfunctions 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 therefore important to distinguish between syntactic features and library features.

注意:一些评论者问为什么更改--target而不是--lib因为两者都可以使代码进行类型检查是错误的?原因是--target改变了代码的转译方式。例如,"target": "es2017"意味着async不会为较旧的运行时转换函数。这是不正确的,因为其意图是启用附加库的使用,而不是更改输出语法,因此区分句法特征和库特征很重要。