[typescript]I don't know why only find() method doesn't work

Hello, I’m studying LEARN TYPESCRIPT: FUNDAMENTALS now.
I’m doing TypeMart in it, then occured weird error again and again.

the link is below:
https://www.codecademy.com/courses/learn-typescript-fundamentals/projects/typemart

I got unstuck at task3,
I used find() method on products for product, but it made error and it is below.

index.ts:12:26 - error TS2550: Property ‘find’ does not exist on type ‘{ name: string; price: string; preOrder: string; }’. Do you need to change your target library? Try changing the ‘lib’ compiler option to ‘es2015’ or later.

12 const product = products.find(p => p.name === productName);

but, when I use filter(), it works. I really don’t know why, and it’s even said to use find() on hint box!
I’ve just started to study Typescript, so many things are new for me.

Please help me.
Thank you.

The difference is that the filter method returns an empty array if no product is found whereas the find method returns ‘undefined’ in that case. So product is a different type when you use find or filter. Until step 3 that should not matter. Have you already written code from the following steps?

1 Like

Thank you for your help.
I got the difference between find and filter.
anyway, I tried that code without following steps but it didn’t work.

I’ll try this again from beginning.

I tried both methods, too, and did not get an error after compiling. Maybe you should post all of your code from index.ts

1 Like

Thanks! I tried again and it works well haha

1 Like

If this topic doesn’t solve your problem, open a new one and post your code in proper format, please.

1 Like

Hi, I got the same error message as mentioned when using the .find method AND compiling with passing index.ts as an argument:
$ tsc index.ts.

When calling
$ tsc
without arguments, compilation works without errors.

A possible cause is explained here:

which leads to here:

The given explanation is, that when passing filename(s) to the compiler, the tsconfg.json is ignored and falls back to some default options, supposedly.

When using the compiler target setting from the lesson’s environment (display via $ cat tsconfig.json):
$ tsc index.ts --lib es2017
the error message changes (to error TS2584, missing ‘console’ name), which indicates the previously reported missing .find methid is not an issue any more.

Hope this helps s.o.

2 Likes

tsc index.ts --target es2017

or just
tsc