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.
I am sure I am missing something!
Join Stack Overflow to learn, share knowledge, and build your career.
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.
I am sure I am missing something!
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
In this case just include the following configuration options in your tsconfig.js
:
...
"lib": [ "es6" ],
"target": "es5"
...
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:
"compilerOptions": {
"lib": ["es6"],
"target": "es5"
}
TypeScript 2.0
As said before, the find
function is indeed part of ES6.
We fixed it by using filter
instead and taking the first element of the resulting array.
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:
{
... (other configs)
"compilerOptions": {
"lib": [ "es2017", "dom" ],
"target": "es5"
}
... (other configs)
}
with "lib": ["es6"] I am getting more errors, less with [ "es2017", "dom" ]:
"compilerOptions": {
"module": "es6",
"lib": [ "es2017", "dom" ]
...............
}
And it solves the problem with [].find()
So... If you use Typescript then you could be using Array.prototype.find() because Typescript is a superset of javascript. BUT because you 'compile' to ES5 you get the error, because the method find is part of ES6 (ES2015), but not ES5.
It took me a while to realize of this obvious fact. It is not your typescript, it is to what you target.
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