Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

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

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);

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

"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": {
    "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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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*.

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

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

For example:

"target": "es2017"

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

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

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.

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

See the TypeScript Handbook for additional details.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...