<p dir="auto">Hi guys, I have searched around existing <code class="notranslate">Elvis Operator</code> in TS and no result has found. That`s why I propose include this ability.</p>
<p dir="auto">For example, like in C# or Groovy we can write next expression</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="let result = somevalue?.name;"><pre class="notranslate"><span class="pl-k">let</span> <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-s1">somevalue</span><span class="pl-kos">?.</span><span class="pl-c1">name</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">it desugared to</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var result = somevalue!==undefined?somevalue.name:undefined;"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-s1">result</span> <span class="pl-c1">=</span> <span class="pl-s1">somevalue</span><span class="pl-c1">!==</span><span class="pl-c1">undefined</span>?<span class="pl-s1">somevalue</span><span class="pl-kos">.</span><span class="pl-c1">name</span>:<span class="pl-c1">undefined</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Is it good idea, or it has already discussed?</p>
<h1 dir="auto">Current Status</h1>
<ul dir="auto">
<li>The TC39 proposal is now at stage 3 (<g-emoji class="g-emoji" alias="tada" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png">🎉</g-emoji><g-emoji class="g-emoji" alias="tada" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png">🎉</g-emoji><g-emoji class="g-emoji" alias="tada" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png">🎉</g-emoji><g-emoji class="g-emoji" alias="tada" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png">🎉</g-emoji><g-emoji class="g-emoji" alias="tada" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f389.png">🎉</g-emoji>)</li>
<li>Implementation is in progress</li>
<li>You can expect this feature in TypeScript 3.7</li>
<li>We'll update here when it's available in a nightly build</li>
<li>Holding off on Optional Call until its semantics are clarified at committee</li>
</ul>
<p dir="auto">Open questions</p>
<ul dir="auto">
<li>What special-casing, if any, should <code class="notranslate">document.all</code> get?</li>
</ul>
<hr>
<p dir="auto">C# and other languages have syntax sugar for accessing property chains where <code class="notranslate">null</code> (or in our case, <code class="notranslate">undefined</code>) might be encountered at any point in the object hierarchy.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var x = { y: { z: null, q: undefined } };
console.log(x?.y?.z?.foo); // Should print 'null'
console.log(x?.baz); // Still an error
console.log(x.y.q?.bar); // Should print 'undefined'"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span> <span class="pl-c1">y</span>: <span class="pl-kos">{</span> <span class="pl-c1">z</span>: <span class="pl-c1">null</span><span class="pl-kos">,</span> <span class="pl-c1">q</span>: <span class="pl-c1">undefined</span> <span class="pl-kos">}</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">?.</span><span class="pl-c1">y</span><span class="pl-kos">?.</span><span class="pl-c1">z</span><span class="pl-kos">?.</span><span class="pl-c1">foo</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// Should print 'null'</span>
<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">?.</span><span class="pl-c1">baz</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// Still an error</span>
<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s1">x</span><span class="pl-kos">.</span><span class="pl-c1">y</span><span class="pl-kos">.</span><span class="pl-c1">q</span><span class="pl-kos">?.</span><span class="pl-c1">bar</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// Should print 'undefined'</span></pre></div>
<p dir="auto">Need proposal on what exactly we should codegen, keeping in mind side effects of accessors.</p>
<hr>
<p dir="auto"><em><strong>Edit by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/DanielRosenwasser/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/DanielRosenwasser">@DanielRosenwasser</a> February 27, 2018:</strong> This proposal is also called the "null propagation" operator.</em></p>