Typescript: Object is possibly ‘undefined’

Issue

Object is possibly ‘undefined’

Answer 1:

Ok, it looks like the culprit here is the use of "strictNullChecks": true. in your config file.

However I would not recommend removing this flag, this check is actually giving you a valid hint so that can use a better null check in your code. Instead, I would look into better ways to doing the null or undefined check.

Answer 2:

There are other two ways to that I can think of to get rid of the error.

The first way I can think of is to use a fallback with the || operator.

The other way is to save the value to a variable and check the new variable.

Leave a Reply

Your email address will not be published. Required fields are marked *