6

With the following code I get an error that the find() property not exist:

var foobar = [];
var bar = { value: 123 };
var foo = { value: 456 };
foobar.push(foo, bar);
console.log(foobar.find((obj) => obj.value === 123));

If I use the same code with javascript it works without problems.

Edit: Maybe a bit dirty workaround... You can cast foobar to any and so dodge the compiler error: console.log( (foobar as any).find((obj) => obj.value === 123));

4
  • It works fine for me. Commented Sep 5, 2019 at 23:11
  • 1
    yihaaa, thanks for a delayed downvote with a so detailed reason -.- Commented Sep 6, 2019 at 4:45
  • I didn't downvote, nor voted to close this --_--. Commented Sep 6, 2019 at 9:59
  • 1
    Upvote, because I searched for it and found here the solution. Please do not downvote without delivering a reason. Commented Jun 27, 2023 at 8:13

4 Answers 4

5

Since TypeScript is a superset of JavaScript, all code should compile to plain JavaScript, so your code should work. If it doesn't for whatever reason, try TypeScript's filter() method instead of find():

foobar.filter(obj => obj.value === 123)[0];
Sign up to request clarification or add additional context in comments.

3 Comments

filter requires iterating over every item, find doesn't.
@jhpratt Using a loop is a simple and painless solution.
I dont know why the compiler was unhappy with find() but filter() works as expected - thanks!
4

I've checked this code. All works fine.

const bar = { value: 123 };
const foo = { value: 456 };
const foobar = [foo, bar];

console.log(foobar.find((obj) => obj.value === 123));

2 Comments

The code works, but does TS recognise the results type?
If type of array and searched item are the same, why not? Set array and item types directly. const arr: Array<number> = ..., const searchedItem: number = ... Something like that.
0

What is giving you the error? Is it your browser, your text editor, or the Typescript compiler?

If it's your browser, you need to change the target in your tsconfig.json. Try es5. Internet Explorer and Node 0.12 don't support Array.find.

If it's your text editor, you need to fix the editor settings. Which setting exactly will depend on the editor. You'll want to find the setting for your Javascript or Typescript language version and set it to es6 or above.

If it's the Typescript compiler... I don't actually know what could cause that. Perhaps a very old version of Typescript?

1 Comment

VS2017 compiler throw the error. Not sure if its a bug but I use now .filter() and it works.
0

As far as I can tell, v5.4.5 doesn't choke on your code, so perhaps something changed. If you are running into this issue on older Typescript, and need a fix, you can use the following:

You have to specify the type of the array object:

var foobar = [];
var bar = { value: 123 };
var foo = { value: 456 };
foobar.push(foo, bar);
console.log(foobar.find((obj: {value:number}) => obj.value === 123));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.