<p dir="auto">To completely fix <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="53324949" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/1587" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/1587/hovercard" href="https://github.com/microsoft/TypeScript/issues/1587">#1587</a> TypeScript should also support:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function repro(message: Object | Object[]) {
    if (message instanceof Array) {
        message = message.filter;  // error
    }
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">repro</span><span class="pl-kos">(</span><span class="pl-s1">message</span>: <span class="pl-smi">Object</span> <span class="pl-c1">|</span> <span class="pl-smi">Object</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">if</span> <span class="pl-kos">(</span><span class="pl-s1">message</span> <span class="pl-k">instanceof</span> <span class="pl-smi">Array</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-s1">message</span> <span class="pl-c1">=</span> <span class="pl-s1">message</span><span class="pl-kos">.</span><span class="pl-c1">filter</span><span class="pl-kos">;</span>  <span class="pl-c">// error</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Currently it results in <code class="notranslate">Property 'entries' does not exist on type 'Object | Object[]'</code>. It seems <code class="notranslate">message</code> on the RHS switches from <code class="notranslate">Array</code> back to <code class="notranslate">Object | Object[]</code> prematurely.</p>
<p dir="auto">Workaround:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function workaround(message: Object | Object[]) {
    var theMessage = message;
    if (theMessage instanceof Array) {
        message = theMessage.filter;
    }
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">workaround</span><span class="pl-kos">(</span><span class="pl-s1">message</span>: <span class="pl-smi">Object</span> <span class="pl-c1">|</span> <span class="pl-smi">Object</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">theMessage</span> <span class="pl-c1">=</span> <span class="pl-s1">message</span><span class="pl-kos">;</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">theMessage</span> <span class="pl-k">instanceof</span> <span class="pl-smi">Array</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-s1">message</span> <span class="pl-c1">=</span> <span class="pl-s1">theMessage</span><span class="pl-kos">.</span><span class="pl-c1">filter</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><strong>TS 1.4</strong></p>
<p dir="auto">Type guard fails to narrow the union type when returning early.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function foo(x: number | string) {
    if (typeof x === 'string') {
        return;
    }
    // x should now be a number
    if (2 % x !== 0) {
        // do something
    }
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-s1">x</span>: <span class="pl-smi">number</span> <span class="pl-c1">|</span> <span class="pl-smi">string</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-k">typeof</span> <span class="pl-s1">x</span> <span class="pl-c1">===</span> <span class="pl-s">'string'</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">return</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
    <span class="pl-c">// x should now be a number</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-c1">2</span> <span class="pl-c1">%</span> <span class="pl-s1">x</span> <span class="pl-c1">!==</span> <span class="pl-c1">0</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-c">// do something</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Unfortunately, I get the following compiler error:</p>
<blockquote>
<p dir="auto">The right-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.</p>
</blockquote>
<p dir="auto">Clearly, the compiler should know at this point that it can't possibly be dealing with a <code class="notranslate">string</code>, as that scenario has already been returned.</p>