<p dir="auto"><strong>TypeScript Version:</strong></p>
<p dir="auto">1.8.0</p>
<p dir="auto"><strong>Code</strong></p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// A self-contained demonstration of the problem follows...
class A {
    constructor(a: number, b: boolean) {

    }
    foo() {

    }
    bar: boolean;
}

function doIt(a: A) {
    if (a instanceof A) {

    }
}

doIt(new A(1, true));

doIt({ foo() { }, bar: false }); // expect type check error"><pre class="notranslate"><span class="pl-c">// A self-contained demonstration of the problem follows...</span>
<span class="pl-k">class</span> <span class="pl-smi">A</span> <span class="pl-kos">{</span>
    <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-s1">a</span>: <span class="pl-smi">number</span><span class="pl-kos">,</span> <span class="pl-s1">b</span>: <span class="pl-smi">boolean</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

    <span class="pl-kos">}</span>
    <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

    <span class="pl-kos">}</span>
    <span class="pl-c1">bar</span>: <span class="pl-smi">boolean</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">function</span> <span class="pl-en">doIt</span><span class="pl-kos">(</span><span class="pl-s1">a</span>: <span class="pl-smi">A</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">a</span> <span class="pl-k">instanceof</span> <span class="pl-smi">A</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-en">doIt</span><span class="pl-kos">(</span><span class="pl-k">new</span> <span class="pl-smi">A</span><span class="pl-kos">(</span><span class="pl-c1">1</span><span class="pl-kos">,</span> <span class="pl-c1">true</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-en">doIt</span><span class="pl-kos">(</span><span class="pl-kos">{</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span> <span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-c1">bar</span>: <span class="pl-c1">false</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// expect type check error</span></pre></div>
<p dir="auto"><strong>Expected behavior:</strong></p>
<p dir="auto">The second to <code class="notranslate">doIt</code> should raise a type check error.</p>
<p dir="auto"><strong>Actual behavior:</strong></p>
<p dir="auto">It does not. I assume it was a conscious decision and I wonder why? This break assumptions the <code class="notranslate">instanceof</code> operator makes and has led to subtle bugs on our side.</p>
<p dir="auto"><strong>TypeScript Version:</strong></p>
<p dir="auto">1.8.x and nightly</p>
<p dir="auto"><strong>Code</strong></p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class C1 { item: string }
class C2 { item: string[] }
class C3 { item: string }

function Foo(x: C1 | C2 | C3): string {
    if (x instanceof C1)
        return x.item;
    else if (x instanceof C2)
        return x.item[0];
    else if (x instanceof C3)
        return x.item; // Error property item does not exist on C2
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">C1</span> <span class="pl-kos">{</span> <span class="pl-c1">item</span>: <span class="pl-smi">string</span> <span class="pl-kos">}</span>
<span class="pl-k">class</span> <span class="pl-smi">C2</span> <span class="pl-kos">{</span> <span class="pl-c1">item</span>: <span class="pl-smi">string</span><span class="pl-kos">[</span><span class="pl-kos">]</span> <span class="pl-kos">}</span>
<span class="pl-k">class</span> <span class="pl-smi">C3</span> <span class="pl-kos">{</span> <span class="pl-c1">item</span>: <span class="pl-smi">string</span> <span class="pl-kos">}</span>

<span class="pl-k">function</span> <span class="pl-smi">Foo</span><span class="pl-kos">(</span><span class="pl-s1">x</span>: <span class="pl-smi">C1</span> <span class="pl-c1">|</span> <span class="pl-smi">C2</span> <span class="pl-c1">|</span> <span class="pl-smi">C3</span><span class="pl-kos">)</span>: <span class="pl-smi">string</span> <span class="pl-kos">{</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-k">instanceof</span> <span class="pl-smi">C1</span><span class="pl-kos">)</span>
        <span class="pl-k">return</span> <span class="pl-s1">x</span><span class="pl-kos">.</span><span class="pl-c1">item</span><span class="pl-kos">;</span>
    <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-k">instanceof</span> <span class="pl-smi">C2</span><span class="pl-kos">)</span>
        <span class="pl-k">return</span> <span class="pl-s1">x</span><span class="pl-kos">.</span><span class="pl-c1">item</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">;</span>
    <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-k">instanceof</span> <span class="pl-smi">C3</span><span class="pl-kos">)</span>
        <span class="pl-k">return</span> <span class="pl-s1">x</span><span class="pl-kos">.</span><span class="pl-c1">item</span><span class="pl-kos">;</span> <span class="pl-c">// Error property item does not exist on C2</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><strong>Expected behavior:</strong><br>
Code should compile</p>
<p dir="auto"><strong>Actual behavior:</strong><br>
Code has an error as shown</p>
<h1 dir="auto">More</h1>
<p dir="auto">The following works i.e. if <code class="notranslate">C1</code> and <code class="notranslate">C3</code> differ in structural compatibility:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class C1 { item: string }
class C2 { item: string[] }
class C3 { items: string }

function Foo(x: C1 | C2 | C3): string {
    if (x instanceof C1)
        return x.item;
    else if (x instanceof C2)
        return x.item[0];
    else if (x instanceof C3)
        return x.items; // Ok
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">C1</span> <span class="pl-kos">{</span> <span class="pl-c1">item</span>: <span class="pl-smi">string</span> <span class="pl-kos">}</span>
<span class="pl-k">class</span> <span class="pl-smi">C2</span> <span class="pl-kos">{</span> <span class="pl-c1">item</span>: <span class="pl-smi">string</span><span class="pl-kos">[</span><span class="pl-kos">]</span> <span class="pl-kos">}</span>
<span class="pl-k">class</span> <span class="pl-smi">C3</span> <span class="pl-kos">{</span> <span class="pl-c1">items</span>: <span class="pl-smi">string</span> <span class="pl-kos">}</span>

<span class="pl-k">function</span> <span class="pl-smi">Foo</span><span class="pl-kos">(</span><span class="pl-s1">x</span>: <span class="pl-smi">C1</span> <span class="pl-c1">|</span> <span class="pl-smi">C2</span> <span class="pl-c1">|</span> <span class="pl-smi">C3</span><span class="pl-kos">)</span>: <span class="pl-smi">string</span> <span class="pl-kos">{</span>
    <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-k">instanceof</span> <span class="pl-smi">C1</span><span class="pl-kos">)</span>
        <span class="pl-k">return</span> <span class="pl-s1">x</span><span class="pl-kos">.</span><span class="pl-c1">item</span><span class="pl-kos">;</span>
    <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-k">instanceof</span> <span class="pl-smi">C2</span><span class="pl-kos">)</span>
        <span class="pl-k">return</span> <span class="pl-s1">x</span><span class="pl-kos">.</span><span class="pl-c1">item</span><span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">;</span>
    <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-s1">x</span> <span class="pl-k">instanceof</span> <span class="pl-smi">C3</span><span class="pl-kos">)</span>
        <span class="pl-k">return</span> <span class="pl-s1">x</span><span class="pl-kos">.</span><span class="pl-c1">items</span><span class="pl-kos">;</span> <span class="pl-c">// Ok</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><g-emoji class="g-emoji" alias="rose" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f339.png">🌹</g-emoji></p>