typescript 任何类型的打字稿或 javascript 数组中都不存在“查找”吗?

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

Doesn't "find" exist on any kind of typescript or javascript array?

typescript

提问by pitosalas

Notice the error message at the bottom: "config.ts(19,28): error TS2339: Property 'find' does not exist on type 'Answer[]". I thought all arrays would have a "find" method.

请注意底部的错误消息:“config.ts(19,28): error TS2339: Property 'find' does not exist on type 'Answer[]”。我认为所有数组都会有一个“查找”方法。

I am sure I am missing something!

我确定我错过了一些东西!

enter image description here

在此处输入图片说明

回答by András Szepesházi

Since Typescript 2.0 you could also use the --libcompiler flag or a "lib": []section in your tsconfig.jsfile to include ES6features, while still targeting ES5. See https://github.com/Microsoft/TypeScript/issues/6974

从 Typescript 2.0 开始,您还可以使用--lib编译器标志或文件中的某个"lib": []部分tsconfig.js来包含ES6功能,同时仍然针对ES5. 见https://github.com/Microsoft/TypeScript/issues/6974

In this case just include the following configuration options in your tsconfig.js:

在这种情况下,只需在您的tsconfig.js:

...
"lib": [ "es6" ],
"target": "es5"
...

回答by codejockie

To answer your question it exists in TypeScript but not without some configurations.
To fix the issue you need to update your compilerOptionsas follows:

要回答您的问题,它存在于 TypeScript 中,但并非没有一些配置。
要解决此问题,您需要compilerOptions按如下方式更新:

"compilerOptions": {
    "lib": ["es6"],
    "target": "es5"
 }

TypeScript 2.0

打字稿 2.0

回答by Iman Mohamadi

Just a wrap up of all other answers. Create a tsconfig.jsonin you project root directory, and make sure you have this in you config:

只是所有其他答案的总结。tsconfig.json在你的项目根目录中创建一个,并确保你的配置中有这个:

{
  ... (other configs)
  "compilerOptions": {
    "lib": [ "es2017", "dom" ],
    "target": "es5"
  }
  ... (other configs)
}

回答by miva2

As said before, the findfunction is indeed part of ES6.

如前所述,该find功能确实是 ES6 的一部分。

We fixed it by using filterinstead and taking the first element of the resulting array.

我们通过使用filter代替并获取结果数组的第一个元素来修复它。

回答by Dmitri Algazin

with "lib": ["es6"] I am getting more errors, less with [ "es2017", "dom" ]:

使用 "lib": ["es6"] 我得到更多错误,更少使用 [ "es2017", "dom"]:

  "compilerOptions": {
       "module": "es6",
        "lib": [ "es2017", "dom" ]
        ...............
  }

And it solves the problem with [].find()

它解决了 [].find() 的问题

回答by Pere Pages

So... If you use Typescript then you could be using Array.prototype.find() because Typescript is a superset of javascript. BUTbecause you 'compile' to ES5 you get the error, because the method findis part of ES6 (ES2015), but not ES5.

所以...如果你使用 Typescript 那么你可以使用 Array.prototype.find() 因为 Typescript 是 javascript 的超集。但是因为你“编译”到 ES5 你会得到错误,因为find方法是 ES6 (ES2015) 的一部分,而不是 ES5。

It took me a while to realize of this obvious fact. It is not your typescript, it is to what you target.

我花了一段时间才意识到这个显而易见的事实。这不是您的打字稿,而是您的目标。

回答by pitosalas

I guess the answer was included in the comment by kkaosninja: find is part of es6 and I am still on es5: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

我想答案包含在 kkaosninja 的评论中: find 是 es6 的一部分,我仍在 es5 上:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/寻找