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

查看:46
本文介绍了“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'"

基于快速 google 看来,解决方案可能是将 compilerOptions 目标属性从 es5 更改为 es6.然而,在对上一期的一些先前研究之后,我认为我能够通过包含额外的lib"来利用 es6 功能.下面是我的 tsconfig.json 上的属性:

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 是正确的方法,但是您指定了错误的语言版本.根据 MDNES2017 规范中正式添加了 Object.entries.

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"]

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

is therefore what you must specify instead*.

如果您希望添加 ES2017中添加的Object函数的方法声明,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": ["es2020"]

甚至自然而然

"lib": ["esnext"] 

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

此外,"lib" 选项的数组性质允许您组合多个值以匹配您的运行时.例如,要将支持 es2015 的 Web 浏览器与添加这些由 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", "es2017.object", "dom"]

有关其他详细信息,请参阅 TypeScript 手册.

See the TypeScript Handbook for additional details.

注意:一些评论者问为什么更改 --target 而不是 --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 therefore important to distinguish between syntactic features and library features.

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

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