<p dir="auto">In TypeScript playground, the "New Features" option gives this code</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="type NameOrNameArray = string | string[];

function createName(name: NameOrNameArray) {
    if (typeof name === &quot;string&quot;) {
        return name;
    }
    else {
        return name.join(&quot; &quot;);
    }
}

var greetingMessage = `Greetings, ${createName([&quot;Sam&quot;, &quot;Smith&quot;]) }`;
alert(greetingMessage);"><pre class="notranslate"><code class="notranslate">type NameOrNameArray = string | string[];

function createName(name: NameOrNameArray) {
    if (typeof name === "string") {
        return name;
    }
    else {
        return name.join(" ");
    }
}

var greetingMessage = `Greetings, ${createName(["Sam", "Smith"]) }`;
alert(greetingMessage);
</code></pre></div>
<p dir="auto">If I make a type such as</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="interface Guid {
  toLowerCase:()=&gt;string;
}"><pre class="notranslate"><code class="notranslate">interface Guid {
  toLowerCase:()=&gt;string;
}
</code></pre></div>
<p dir="auto">(since I'm using TypeLite to get some C# types in my TypeScript I have a Guid type but basically treat Guids as strings so toLowerCase() is enough to discriminate for my purposes).</p>
<p dir="auto">Anyway, if I then modify the playground code to be</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="interface Guid {
  toLowerCase:()=&gt;string;
}
type GuidOrGuidArray = Guid | Guid[];

function createGuid(g: GuidOrGuidArray) {
    if (g instanceof Array) {
      //intellisense on g.  should give me array things
      var x = g[0];     //This SHOULD compile
   }
    else {
        //intellisense on g.  should give me toLowerCase() as my only option
        var y = g.toLowerCase();   ///This SHOULD compile
    }
}
"><pre class="notranslate"><code class="notranslate">interface Guid {
  toLowerCase:()=&gt;string;
}
type GuidOrGuidArray = Guid | Guid[];

function createGuid(g: GuidOrGuidArray) {
    if (g instanceof Array) {
      //intellisense on g.  should give me array things
      var x = g[0];     //This SHOULD compile
   }
    else {
        //intellisense on g.  should give me toLowerCase() as my only option
        var y = g.toLowerCase();   ///This SHOULD compile
    }
}

</code></pre></div>
<p dir="auto">Instead I get the type of <code class="notranslate">x</code> to be any and <code class="notranslate">y</code> fails to compile.  In both cases, the popup hint in the playground (and in Visual Studio) gives the indication that the type of g within each branch of the if is still the union type.</p>
<p dir="auto">You'll note that I switched the if block around so that I'm using instanceof Array rather than typeof === "string".    That's because I've tried it the other way and wasn't surprised that it didn't work since my Guid interface doesn't extend string in any way and typeof [] gives object.</p>
<p dir="auto">Should this work?  At the moment I'm doing the kludge workarounds...<br>
<code class="notranslate">var x = &lt;Guid[]&gt;g[0];</code><br>
and<br>
<code class="notranslate">var y = (&lt;Guid&gt;g).toLowerCase();</code></p>
<p dir="auto">It'd be <g-emoji class="g-emoji" alias="100" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f4af.png">💯</g-emoji> if instanceof Array worked to discriminate between the array and non-array in the union type.</p>
<p dir="auto">Hi,<br>
this is and issue I had a few times upgrading aurelia packages during the preview time, but I think may a problem even in production, because looks like that frameworks like aurelia and angular 2 are going to add .d.ts files inside the packages.<br>
the problem is that upgrading the standard set of aurelia packages, I can have another additional package that still references an older version of some standard packages, as aurelia-templating and so on.<br>
At runtime I don't see any problem, because module loaders can handle multiple versions of the package, but there is the problem that inside the project you have two versions of the same .d.ts file almost identical.<br>
How can I handle this?</p>