Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.find (and other similar methods) does not implicitly return `undefined`

Array.prototype.find must return T | undefined.

But for some reason in my (automatically generated NestJS) environment TypeScript implicitly has only T as the return type of such methods.

How should I make TypeScript automatically inferr that find must return T | undefined?

like image 982
Washington Irving Avatar asked Oct 26 '25 13:10

Washington Irving


1 Answers

Take a look at strictNullChecks compiler option: https://www.typescriptlang.org/tsconfig#strictNullChecks

When strictNullChecks is false, null and undefined are effectively ignored by the language. This can lead to unexpected errors at runtime.

When strictNullChecks is true, null and undefined have their own distinct types and you’ll get a type error if you try to use them where a concrete value is expected.

like image 200
turtlepick Avatar answered Oct 29 '25 01:10

turtlepick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!