<p dir="auto">nightly build</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="const x = Math.random() &gt; 0.5 ? 'hey' : 1;
if (typeof x === 'number') {
    [].map(() =&gt; x + 2); // expected should typecheck, actual no-no-no
}"><pre class="notranslate"><code class="notranslate">const x = Math.random() &gt; 0.5 ? 'hey' : 1;
if (typeof x === 'number') {
    [].map(() =&gt; x + 2); // expected should typecheck, actual no-no-no
}
</code></pre></div>
<p dir="auto">numbers can't mutate, <code class="notranslate">x</code> holds a number, also it is a constant, constant can't change its value, so ...</p>
<p dir="auto">This is a place holder for changes to the type narrowing behavior in TypeScript 2.0:</p>
<ul dir="auto">
<li>Generic type parameters are now narrowed</li>
</ul>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function g&lt;T&gt;(obj: T) {
    var t: T;
    if (obj instanceof RegExp) {
         t = obj; // RegExp is not assignable to T
    }
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">g</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span><span class="pl-kos">(</span><span class="pl-s1">obj</span>: <span class="pl-smi">T</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">var</span> <span class="pl-s1">t</span>: <span class="pl-smi">T</span><span class="pl-kos">;</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">obj</span> <span class="pl-k">instanceof</span> <span class="pl-smi">RegExp</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
         <span class="pl-s1">t</span> <span class="pl-c1">=</span> <span class="pl-s1">obj</span><span class="pl-kos">;</span> <span class="pl-c">// RegExp is not assignable to T</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">See the explanation by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/Arnavion/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/Arnavion">@Arnavion</a> in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="144126851" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/7719" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/7719/hovercard?comment_id=202684512&amp;comment_type=issue_comment" href="https://github.com/microsoft/TypeScript/issues/7719#issuecomment-202684512">#7719 (comment)</a></p>
<ul dir="auto">
<li>Type narrowing does not work for catprued variables in functions and class expressions</li>
</ul>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="export class Class {
    constructor(public p) { }
}

var c: Class | string;
var x: Class;

if (c instanceof Class) {
    function inner() {
        x = c; // Error, type of c is not narrowed, c is Class | string
    }
    x = c; // OK, c is Class
}"><pre class="notranslate"><span class="pl-k">export</span> <span class="pl-k">class</span> <span class="pl-smi">Class</span> <span class="pl-kos">{</span>
    <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-k">public</span> <span class="pl-s1">p</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-k">var</span> <span class="pl-s1">c</span>: <span class="pl-smi">Class</span> <span class="pl-c1">|</span> <span class="pl-smi">string</span><span class="pl-kos">;</span>
<span class="pl-k">var</span> <span class="pl-s1">x</span>: <span class="pl-smi">Class</span><span class="pl-kos">;</span>

<span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">c</span> <span class="pl-k">instanceof</span> <span class="pl-smi">Class</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">function</span> <span class="pl-en">inner</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">c</span><span class="pl-kos">;</span> <span class="pl-c">// Error, type of c is not narrowed, c is Class | string</span>
    <span class="pl-kos">}</span>
    <span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">c</span><span class="pl-kos">;</span> <span class="pl-c">// OK, c is Class</span>
<span class="pl-kos">}</span></pre></div>