DEV Community

Discussion on: What is wrong with optional chaining and how to fix it

 
vcicmanec profile image
vcicmanec • Edited

You'd need to do it anyway. It's not the fault of optional chaining, it's the fault of dynamic types and truthy-/falsy-ness being baked into the language.

If number of characters is your worry, just use if (z > -1). That will even do a limited type check for you, as objects will always fail.

Thread Thread
 
macsikora profile image
Pragmatic Maciej • Edited

Yes if you talk about the condition issue exactly. The issue is that optional chaining need to be used with previous bad parts baked into language, and because the whole chain checks null | undefined but the last evaluated value put into if will behave by the falsy | Truthy.

But the second is not fully related. The issue is you cannot execute safetly function on the result, you need to always do additional if before, so again it is error prone.