What is the reason behind the way the TypeScript compiler translates its optional chaining and null-coalescing operators, found here, from:
// x?.y
x === null || x === void 0 ? void 0 : x.y;
// x ?? y
x !== null && x !== void 0 ? x : y
as opposed to:
// x?.y
x == null ? void 0 : x.y
// x ?? y
x != null ? x : y
It seems like a single check would have sufficed for both cases, potentially making the code cleaner. Additionally, shouldn't optional chaining compile to:
x == null ? x : x.y
in order to maintain the distinction between null
and undefined
? There is further discussion on this topic in: Why does JavaScript's optional chaining use undefined instead of preserving null?