Call new Date with union type "number | string"
<p>Consider the following snippet (see also <a href="http://www.typescriptlang.org/Playground#src=var%20nr%3A%20number%20%3D%20123456%3B%0D%0Avar%20str%3A%20string%20%3D%20%222015-01-01T12%3A00%3A00%22%3B%0D%0Avar%20both%3A%20number%20%7C%20string%20%3D%20123456%3B%0D%0A%0D%0Avar%20myDate%3A%20Date%3B%0D%0A%0D%0AmyDate%20%3D%20new%20Date%28nr%29%3B%0D%0AmyDate%20%3D%20new%20Date%28str%29%3B%0D%0AmyDate%20%3D%20new%20Date%28both%29%3B%20%2F%2F%20%3C--%20Compile%20error" rel="noreferrer">TS Playground</a>):</p>

<pre><code>var nr: number = 123456;
var str: string = "2015-01-01T12:00:00";
var both: number | string = 123456;

var myDate: Date;

myDate = new Date(nr);
myDate = new Date(str);
myDate = new Date(both); // &lt;-- Compile error
</code></pre>

<p>That final line gives a compiler error:</p>

<blockquote>
  <p>Argument of type <code>number | string</code> is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'.</p>
</blockquote>

<p>However, since there's a <code>Date(...)</code> constructor for both types I'd assume the above would work.</p>

<p>I can workaround the issue because there is another constructor taking <code>any</code> parameter:</p>

<pre><code>myDate = new Date(&lt;any&gt; both);
</code></pre>

<p>But what if that constructor wasn't there, e.g. if this scenario occurred in my own class?</p>

<p>Is there any way to get this to work properly? Or is the union type a design smell here indicating my definitions need to be changed?</p>

<p>I've checked the <a href="http://www.typescriptlang.org/Handbook" rel="noreferrer">TS Handbook</a> but it has no section on union types. I've tried solving it myself but didn't get past the <code>&lt;any&gt;</code> trick mentioned above. I've gone through the suggested duplicates and similar questions on SO but did not find an answer so far.</p>