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
Doesn't "find" exist on any kind of typescript or javascript array?
提问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!
我确定我错过了一些东西!
回答by András Szepesházi
Since Typescript 2.0 you could also use the --lib
compiler flag or a "lib": []
section in your tsconfig.js
file to include ES6
features, 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 compilerOptions
as 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.json
in 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 find
function is indeed part of ES6.
如前所述,该find
功能确实是 ES6 的一部分。
We fixed it by using filter
instead 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/寻找