0

I'm using typescript on a react project and I just upgraded from Typescript 3.7.2 to 3.8. Now I'm getting an Object is possibly 'undefined'. everywhere in the code base where I am doing something line this:

let fieldType = currentFilter ? currentFilter.fieldType : "TEXT";

Here is what the error looks like in VS code enter image description here

Any suggestions on why this isn't working now?

  • You can just add an exclamation to the end to tell typescript that currentFilter is not null or undefined i.e. `currentFilter!.fieldType` – WebbH Mar 25 '21 at 20:40
  • Seems like a bug in your IDE? I checked a few different versions and all seems to work [in the TS playground](https://tsplay.dev/ND5Dxm). When you have an error that shouldn't be happening, I have to make the obvious suggestion: turn everything off an on again. – Linda Paiste Mar 25 '21 at 20:45
  • Use if else statement You will be able to achieve it.Because Ternary operator does not correctly narrow types (while equivalent if statement does). Check this similar issue: https://stackoverflow.com/questions/54884488/how-can-i-solve-the-error-ts2532-object-is-possibly-undefined – shubham yadav Mar 25 '21 at 20:53

0 Answers0