I am facing a challenge where I need to assign a value conditionally to a const. The task involves checking if a nullable string property in an object contains another nullable string property. Depending on the result of this check, I will then assign the const a value from either functionA or functionB. I have attempted to use both 'indexOf' and 'includes', but I keep encountering errors in TypeScript such as 'Object is possibly undefined' or 'expression expected'. If you have any suggestions on how to approach this problem by properly handling the nullability of the object and its properties while evaluating the condition, please share your insights. Any help would be greatly appreciated!
When using indexOf method, TypeScript shows an error indicating that Object might be 'undefined'
const value = (req.object?.propertyA?.indexOf(req.object?.propertyB?) >= 0) ?
functionA(req.object?.propertyA?) :
functionB(req.object?.propertyB?)
On attempting to use includes method to search for the substring, TypeScript throws an error asking for an expression
const value = req.object?.propertyA?.includes(req.object?.propertyB?) ?
functionA(req.object?.propertyA?) :
functionB(req.object?.propertyB?)