<p dir="auto">Hi, I faced a problem unable to cast an object as a method. While this is handled automatically using an if / else block (and congratulation to the guys who implemented this), this is not working yet into a switch case nor manually. Here is <a href="http://www.typescriptlang.org/Playground#src=%0Ainterface%20MethodInterface%7B%0A%20%20%28%29%3A%20string%3B%0A%7D%0A%0Ainterface%20FocusRule%0A%7B%0A%09surprise%3F%3AMethodInterface%7Cstring%0A%7D%0A%0Afunction%20test%28anObject%3AFocusRule%29%0A%7B%0A%09var%20actionOrString%3AMethodInterface%7Cstring%20%3D%20anObject.surprise%3B%0A%09%0A%09%20%20%20%20%20%20%20%20%20%20switch%28typeof%20actionOrString%29%0A%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F**%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20*%20There%20is%20no%20way%20to%20prevent%20the%20compilation%20error%20here%20%28even%20casting%20the%20actionOrString%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22function%22%20%3A%20%3CMethodInterface%3EactionOrString%28%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F**%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20*%20If%20a%20string%2C%20maybe%20we%20are%20using%20a%20helper%20or%20a%20query%20selector%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20*%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20case%20%22string%22%20%20%20%3A%20return%3B%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%09%0A%09%0A%09%2F%2F%20Using%20a%20if%20%2F%20else%20is%20however%20ok%0A%09%0A%09if%20%28typeof%20actionOrString%20%3D%3D%3D%20%22function%22%29%0A%09%7B%0A%09%09actionOrString%28%29%3B%0A%09%7D%0A%09else%0A%09%7B%0A%09%09%2F%2F%20Here%20this%20is%20normal%20to%20fail%E2%80%A6%0A%09%09actionOrString%28%29%3B%0A%09%7D%0A%7D%0A%0A%0A" rel="nofollow">my code</a> to reproduce the error:</p>
<p dir="auto">I have the following code:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function logNumber(v: number) { console.log(&quot;number:&quot;, v); }
function logString(v: string) { console.log(&quot;string:&quot;, v); }

function foo1(v: number|string) {
    switch (typeof v) {
        case 'number':
            logNumber(v);
            break;
        case 'string':
            logString(v);
            break;
        default:
            throw new Error(&quot;unsupported type&quot;);
    }
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">logNumber</span><span class="pl-kos">(</span><span class="pl-s1">v</span>: <span class="pl-smi">number</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-s">"number:"</span><span class="pl-kos">,</span> <span class="pl-s1">v</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-kos">}</span>
<span class="pl-k">function</span> <span class="pl-en">logString</span><span class="pl-kos">(</span><span class="pl-s1">v</span>: <span class="pl-smi">string</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-s">"string:"</span><span class="pl-kos">,</span> <span class="pl-s1">v</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-kos">}</span>

<span class="pl-k">function</span> <span class="pl-en">foo1</span><span class="pl-kos">(</span><span class="pl-s1">v</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">switch</span> <span class="pl-kos">(</span><span class="pl-k">typeof</span> <span class="pl-s1">v</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">case</span> <span class="pl-s">'number'</span>:
            <span class="pl-en">logNumber</span><span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
            <span class="pl-k">break</span><span class="pl-kos">;</span>
        <span class="pl-k">case</span> <span class="pl-s">'string'</span>:
            <span class="pl-en">logString</span><span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
            <span class="pl-k">break</span><span class="pl-kos">;</span>
        <span class="pl-k">default</span>:
            <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-smi">Error</span><span class="pl-kos">(</span><span class="pl-s">"unsupported type"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Argument of type 'string | number' is not assignable to parameter of type 'number'.
 Type 'string' is not assignable to type 'number'."><pre class="notranslate"><code class="notranslate">Argument of type 'string | number' is not assignable to parameter of type 'number'.
 Type 'string' is not assignable to type 'number'.
</code></pre></div>
<p dir="auto">I was forced to rewrite this using <code class="notranslate">if</code> statements.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function foo2(v: number|string) {
    if (typeof v === 'number') {
        logNumber(v);
    } else if (typeof v === 'string') {
        logString(v);
    } else {
        throw new Error(&quot;unsupported type&quot;);
    }
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">foo2</span><span class="pl-kos">(</span><span class="pl-s1">v</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">v</span> <span class="pl-c1">===</span> <span class="pl-s">'number'</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-en">logNumber</span><span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</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-k">typeof</span> <span class="pl-s1">v</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-en">logString</span><span class="pl-kos">(</span><span class="pl-s1">v</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span> <span class="pl-k">else</span> <span class="pl-kos">{</span>
        <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-smi">Error</span><span class="pl-kos">(</span><span class="pl-s">"unsupported type"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Please allow using switch statements as type guards.</p>