<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>
<p dir="auto">I'm using a tsconfig file in our project: <a href="https://github.com/exceptionless/Exceptionless.Javascript">https://github.com/exceptionless/Exceptionless.Javascript</a> and I'm  seeing the following behavior (Everything is in the same module, but multiple files..).</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/1020579/7353128/e3f0da9c-ecd6-11e4-8efb-ad52791af2b2.png"><img src="https://cloud.githubusercontent.com/assets/1020579/7353128/e3f0da9c-ecd6-11e4-8efb-ad52791af2b2.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/1020579/7353130/e7d4585a-ecd6-11e4-9414-7207118fb70b.png"><img src="https://cloud.githubusercontent.com/assets/1020579/7353130/e7d4585a-ecd6-11e4-9414-7207118fb70b.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">I feel that all code should be placed into the same module region when it's generated.</p>