Indeed, the two proposal seems related, but accomplish quite different goals.
? operator aims to give another syntactic sugar to handle nullish values, in the same way than the nullish coalescing
?? or the optional chaining
?. This gives programmers an additional tool they can use wherever they want.
On the other hand, the non-null
! operator gives a syntactic sugar for a particular condition (checking nullish value) of specific situation (assertion). This can indeed be useful, but assertion still doesn't have a native way to be done in the language. I think it would be better to first have a general solution for assertion (like using an
assert keyword or something), before trying to implement syntactic sugar for a sub case of it.
Also, thanks to the non-nullish
? operator general purpose, it could be used in the case of assertion too, maybe removing that need of syntactic sugar for the case of nullish values, and also working with any assertion libraries. For example let's take the case of node
assert function, expecting a boolean as its parameter and checking if it is truthy. Here, treating the case of nullish value becomes dead simple:
// assert that `myVar` is not nullish