<p dir="auto">See <a href="http://www.typescriptlang.org/Playground#src=function%20test%20%28myParam%3A%20string%20%7C%20string%5B%5D%29%3A%20boolean%20%7B%0A%09if%20%28typeof%20myParam%20%3D%3D%3D%20'string'%29%20%7B%0A%09%09if%20%28myParam%20%3D%3D%3D%20'*'%29%20%2F%2F%20type%20here%20is%20%60string%20%7C%20string%5B%5D%60%2C%20but%20should%20be%20%60string%60%0A%09%09%09return%20true%3B%0A%09%09%09%0A%09%09myParam%20%3D%20%5BmyParam%5D%3B%20%2F%2Ferror%0A%09%7D%0A%09%0A%09return%20myParam.length%20%3E%200%3B%0A%7D" rel="nofollow">Playgound</a>:</p>
<p dir="auto">I was sure I had the type guard syntax correct, but it doesn't seem to be working:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function test (myParam: string | string[]): boolean {
    if (typeof myParam === 'string') {
        if (myParam === '*') // type here is `string | string[]`, but should be `string`
            return true;

        myParam = [myParam]; //error
    }

    return myParam.length &gt; 0;
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">test</span> <span class="pl-kos">(</span><span class="pl-s1">myParam</span>: <span class="pl-s1">string</span> <span class="pl-c1">|</span> <span class="pl-s1">string</span><span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">)</span>: <span class="pl-s1">boolean</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">myParam</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">if</span> <span class="pl-kos">(</span><span class="pl-s1">myParam</span> <span class="pl-c1">===</span> <span class="pl-s">'*'</span><span class="pl-kos">)</span> <span class="pl-c">// type here is `string | string[]`, but should be `string`</span>
            <span class="pl-k">return</span> <span class="pl-c1">true</span><span class="pl-kos">;</span>

        <span class="pl-s1">myParam</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-s1">myParam</span><span class="pl-kos">]</span><span class="pl-kos">;</span> <span class="pl-c">//error</span>
    <span class="pl-kos">}</span>

    <span class="pl-k">return</span> <span class="pl-s1">myParam</span><span class="pl-kos">.</span><span class="pl-c1">length</span> <span class="pl-c1">&gt;</span> <span class="pl-c1">0</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Is this a bug, or am I missing something here?</p>
<p dir="auto">it would be nice to support that kind of construction :</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function myFunction(arr: string | string[]) {
    if (typeof arr === 'string') {
        arr = [arr]
    }
    //arr is now string[]
}"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">myFunction</span><span class="pl-kos">(</span><span class="pl-s1">arr</span>: <span class="pl-smi">string</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-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">arr</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-s1">arr</span> <span class="pl-c1">=</span> <span class="pl-kos">[</span><span class="pl-s1">arr</span><span class="pl-kos">]</span>
    <span class="pl-kos">}</span>
    <span class="pl-c">//arr is now string[]</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">It's a common pattern used by library that take options in parameter.<br>
Generally you can pass an array of string or a string if you have a single value, and internally the library will create an array in any case.</p>