title: Why does the TypeScript compiler compile its optional chaining and null-coalescing operators with two checks?
body: <p>Why does the TypeScript compiler compile its <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html" rel="noreferrer">optional chaining and null-coalescing</a> operators, <code>?.</code> and <code>??</code>, to</p>
  <pre class="lang-js prettyprint-override"><code>// x?.y
  x === null || x === void 0 ? void 0 : x.y;
  
  // x ?? y
  x !== null &amp;&amp; x !== void 0 ? x : y
  </code></pre>
  <p>instead of</p>
  <pre class="lang-js prettyprint-override"><code>// x?.y
  x == null ? void 0 : x.y
  
  // x ?? y
  x != null ? x : y
  </code></pre>
  <p>?</p>
  <p>Odds are that behind the scenes <code>== null</code> does the same two checks, but even for the sake of code length, it seems like single check would be cleaner. It adds many fewer parentheses when using a string of optional chaining, too.</p>
  <p>Incidentally, I'm also surprised that optional chaining doesn't compile to</p>
  <pre class="lang-js prettyprint-override"><code>x == null ? x : x.y
  </code></pre>
  <p>to preserve <code>null</code> vs <code>undefined</code>.  This has since been answered: <a href="https://stackoverflow.com/q/69856082/3120446">Why does JavaScript&#39;s optional chaining to use undefined instead of preserving null?</a></p>