Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

typescript

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!

enter image description here

like image 710
pitosalas Avatar asked Jul 13 '15 16:07

pitosalas


People also ask

How do you find if an element exists in an array in TypeScript?

To check if a TypeScript array contains an object:Pass a function to the Array. find() method. Check whether the identifier of the object is equal to a specific value and return true if it is. Array.

Does not exist on TypeScript?

The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string.

What does find return in TypeScript?

The find() method returns the value of the first element that passes a test.


2 Answers

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" ... 
like image 168
András Szepesházi Avatar answered Oct 07 '22 09:10

András Szepesházi


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

like image 27
codejockie Avatar answered Oct 07 '22 09:10

codejockie