<p dir="auto">A very nice addition to TypeScript's type system would be sum types in the spirit of ML-like languages.  This is one of basic and simple programming constructs from functional programming which you really miss once you get used to it, but which seem to have a hard time being included in new modern languages (contrary to other features from functional programming such as first-class functions, structural types, generics).</p>
<p dir="auto">I guess the most natural way to integrate sum types in the current language syntax would be to extend enum variants with extra parameters (similarly to what Rust does:  <a href="http://doc.rust-lang.org/tutorial.html#enums" rel="nofollow">http://doc.rust-lang.org/tutorial.html#enums</a> ) and upgrade the switch statement to a more powerful structural pattern matching (although in a first step, simply discriminating on the toplevel variant and capturing its parameters would be already quite good).</p>
<p dir="auto">This is quite different from other the proposal about "union types"  (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="37905058" data-permission-text="Title is private" data-url="https://github.com/microsoft/TypeScript/issues/14" data-hovercard-type="issue" data-hovercard-url="/microsoft/TypeScript/issues/14/hovercard" href="https://github.com/microsoft/TypeScript/issues/14">#14</a>), which would mostly be useful to capture types in existing Javascript APIs.  Sum types are rather used to describe algebraic data structures. They would be particularly useful for any kind of symbolic processing (including for implementing the TypeScript compiler).</p>
<p dir="auto">Many values or parameters in JavaScript can be of more than one type. For example, a function might take an object where one of the properties can be either a string or a number, but not a function.</p>
<p dir="auto">Comments from Ron Buckton in <a href="https://typescript.codeplex.com/workitem/1364" rel="nofollow">https://typescript.codeplex.com/workitem/1364</a><br>
I'd like to see type annotations provide some support for a type union. For example:</p>
<h3 dir="auto">ES6 Promise</h3>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class Promise&lt;T&gt; {
  // ...
  static any&lt;TResult&gt;(...values: union { Promise&lt;T&gt;; T; }[]): Promise&lt;T&gt;;
  static every&lt;TResult&gt;(...values: union { Promise&lt;T&gt;; T; }[]): Promise&lt;T[]&gt;;
  static some&lt;TResult&gt;(...values: union { Promise&lt;T&gt;; T; }[]): Promise&lt;T&gt;;
  then(resolve?: (value: T) =&gt; union { Promise&lt;T&gt;; T; }, reject?: (value: any) =&gt; union { Promise&lt;T&gt;; T; }): Promise&lt;T&gt;;
  // ...
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-c">// ...</span>
  <span class="pl-k">static</span> <span class="pl-en">any</span><span class="pl-c1">&lt;</span><span class="pl-smi">TResult</span><span class="pl-c1">&gt;</span><span class="pl-kos">(</span>...<span class="pl-s1">values</span>: <span class="pl-smi">union</span><span class="pl-kos"></span> <span class="pl-kos">{</span> <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span> <span class="pl-smi">T</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>
  <span class="pl-k">static</span> <span class="pl-s1">every</span><span class="pl-c1">&lt;</span><span class="pl-smi">TResult</span><span class="pl-c1">&gt;</span><span class="pl-kos">(</span>...<span class="pl-s1">values</span>: <span class="pl-smi">union</span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-smi">T</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>
  <span class="pl-k">static</span> <span class="pl-s1">some</span><span class="pl-c1">&lt;</span><span class="pl-smi">TResult</span><span class="pl-c1">&gt;</span><span class="pl-kos">(</span>...<span class="pl-s1">values</span>: <span class="pl-smi">union</span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-smi">T</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>
  <span class="pl-s1">then</span><span class="pl-kos">(</span><span class="pl-s1">resolve</span>?: <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">T</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">union</span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-c1">T</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-s1">reject</span>?: <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">any</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-smi">union</span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-c1">T</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">)</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>
  <span class="pl-c">// ...</span>
<span class="pl-kos">}</span></pre></div>
<h3 dir="auto">ES6 Loader</h3>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="class Loader&lt;T&gt; {
  // ...
  normalize(name: string, referer?: Referer): union { string; { normalized: string; metadata?: any }; };
  resolve(normalized: string, options?: { referer: Referer; metadata: any; }): union { string; { address: string; extra?: string[]; }; };
  // ...
}"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-smi">Loader</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-c">// ...</span>
  <span class="pl-en">normalize</span><span class="pl-kos">(</span><span class="pl-s1">name</span>: <span class="pl-smi">string</span><span class="pl-kos">,</span> <span class="pl-s1">referer</span>?: <span class="pl-smi">Referer</span><span class="pl-kos">)</span>: <span class="pl-smi">union</span> <span class="pl-kos">{</span> <span class="pl-s1">string</span><span class="pl-kos">;</span> <span class="pl-kos">{</span> normalized: <span class="pl-s1">string</span><span class="pl-kos">;</span> metadata?: <span class="pl-s1">any</span> <span class="pl-kos">}</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>
  <span class="pl-en">resolve</span><span class="pl-kos">(</span><span class="pl-s1">normalized</span>: <span class="pl-smi">string</span><span class="pl-kos">,</span> <span class="pl-s1">options</span>?: <span class="pl-kos">{</span> <span class="pl-c1">referer</span>: <span class="pl-smi">Referer</span><span class="pl-kos">;</span> <span class="pl-c1">metadata</span>: <span class="pl-smi">any</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">)</span>: <span class="pl-smi">union</span> <span class="pl-kos">{</span> <span class="pl-s1">string</span><span class="pl-kos">;</span> <span class="pl-kos">{</span> address: <span class="pl-s1">string</span><span class="pl-kos">;</span> extra?: <span class="pl-s1">string</span><span class="pl-kos">[</span><span class="pl-s1"></span><span class="pl-kos">]</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>
  <span class="pl-c">// ...</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">When static type checking is performed, it is possible to have some type issues when explicitly providing generic type arguments and having the wrong type chosen, but this exists today without supporting type unions.</p>
<p dir="auto">The other open issue is what to do with a local that is a type union: should it act as an <code class="notranslate">any</code>, a type that contains all of the members of both (or all) types in the union, or a type that only contains the members that are the same in both (or all) types in the union.</p>
<p dir="auto">An implicit or explicit type cast from a type union to one of the union types would pass without the need to perform an intermediate cast to , and an explicit type cast to a more specific type for any of the union types would likewise succeed without an intermediate cast.</p>
<p dir="auto">Assignment to a local or field that is a type union would succeed as if it were either of the types (e.g. implicit or explicit type cast from a more specific type to a less specific type specified in the type union).</p>
<p dir="auto">There is also a question on how to properly handle the intellisense for a symbol that uses a type union. It could either be represented as a number of overloads (similar to what would have to be typed today), or preserve the type union definition.</p>
<h2 dir="auto">Union Declaration</h2>
<p dir="auto">Providing a typedef-like syntax for unions would also be useful to define a reusable definition:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="union Ref&lt;T&gt; {
  Promise&lt;T&gt;;
  T;
}"><pre class="notranslate"><span class="pl-s1">union</span> <span class="pl-smi">Ref</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span>
  <span class="pl-smi">T</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">This is roughly analogous to an interface that defines multiple call signatures:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// as an interface with call signatures...
interface ResolveCallback&lt;T&gt; {
  (value: Promise&lt;T&gt;): void;
  (value: T): void;
  (): void;
}

// ...and as a union with function types
union ResolveCallback&lt;T&gt; {
  (value: Promise&lt;T&gt;) =&gt; void;
  (value: T) =&gt; void;
  () =&gt; void;
}"><pre class="notranslate"><span class="pl-c">// as an interface with call signatures...</span>
<span class="pl-k">interface</span> <span class="pl-smi">ResolveCallback</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">)</span>: <span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">;</span>
  <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">T</span><span class="pl-kos">)</span>: <span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">;</span>
  <span class="pl-kos">(</span><span class="pl-kos">)</span>: <span class="pl-smi"><span class="pl-k">void</span></span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-c">// ...and as a union with function types</span>
<span class="pl-s1">union</span><span class="pl-kos"></span> <span class="pl-smi">ResolveCallback</span><span class="pl-c1">&lt;</span><span class="pl-smi">T</span><span class="pl-c1">&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">&gt;</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-k">void</span><span class="pl-kos">;</span>
  <span class="pl-kos">(</span><span class="pl-s1">value</span>: <span class="pl-smi">T</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-k">void</span><span class="pl-kos">;</span>
  <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-k">void</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<h2 dir="auto">Static Analysis</h2>
<p dir="auto">Adding type unions would require changes to the static type information to be supported. The primary goal of adding type unions is to help the compiler determine the best matching type for a call expression or return type expression. The following sections discuss various ways of handling static analysis of type unions.</p>
<h3 dir="auto">Assigning to a Type Union</h3>
<p dir="auto">When assigning to an identifier that is annotated with a type union, passing a value as an argument to a function for a parameter that is a type union, returning a value from a function with a type union in its return type annotation, or type-casting a value to a type union, the type of the value being assigned or returned must be compatible (as either an exact match or a superset of type information) with one of the types defined in the type union.</p>
<p dir="auto">For example:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// assign to variable
var value: union { Promise&lt;number&gt;; number; };
value = 1; // legal
value = Promise.resolve&lt;number&gt;(1); // legal
value = new Date(); // static error

// type-cast to union
declare var n: number;
declare var p: Promise&lt;number&gt;;
declare var a: any;
declare var d: Date;
&lt;union { Promise&lt;number&gt;; number; }&gt;n; // legal
&lt;union { Promise&lt;number&gt;; number; }&gt;p; // legal
&lt;union { Promise&lt;number&gt;; number; }&gt;a; // legal
&lt;union { Promise&lt;number&gt;; number; }&gt;d; // legal

// union in return value
function func(type: string): union { Promise&lt;number&gt;; number; } {
  switch (type) {
    case &quot;number&quot;:
      return 1; // legal

    case &quot;promise&quot;:
     return Promise.resolve&lt;number&gt;(1); // legal

    case &quot;date&quot;:
     return new Date(); // static error  
  }
}

// union in invocation expression
declare function func(promiseOrValue: union { Promise&lt;number&gt;; number; }): void;
declare var n: number;
declare var p: Promise&lt;number&gt;;
declare var a: any;
declare var d: Date;

func(n); // legal
func(p); // legal
func(a); // legal
func(d); // static error"><pre class="notranslate"><span class="pl-c">// assign to variable</span>
<span class="pl-k">var</span> <span class="pl-s1">value</span>: <span class="pl-smi">union</span><span class="pl-kos"></span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">number</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-s1">number</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-s1">value</span> <span class="pl-c1">=</span> <span class="pl-c1">1</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-s1">value</span> <span class="pl-c1">=</span> <span class="pl-smi">Promise</span><span class="pl-kos">.</span><span class="pl-en">resolve</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-kos">(</span><span class="pl-c1">1</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-s1">value</span> <span class="pl-c1">=</span> <span class="pl-k">new</span> <span class="pl-smi">Date</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// static error</span>

<span class="pl-c">// type-cast to union</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">n</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">p</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">a</span>: <span class="pl-smi">any</span><span class="pl-kos">;</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">d</span>: <span class="pl-smi">Date</span><span class="pl-kos">;</span>
<span class="pl-kos">&lt;</span><span class="pl-s1">union</span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">number</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-c1">number</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">&gt;</span><span class="pl-s1">n</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-kos">&lt;</span><span class="pl-s1">union</span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">number</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-c1">number</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">&gt;</span><span class="pl-s1">p</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-kos">&lt;</span><span class="pl-s1">union</span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">number</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-c1">number</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">&gt;</span><span class="pl-s1">a</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-kos">&lt;</span><span class="pl-s1">union</span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">number</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-c1">number</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">&gt;</span><span class="pl-s1">d</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>

<span class="pl-c">// union in return value</span>
<span class="pl-k">function</span> <span class="pl-en">func</span><span class="pl-kos">(</span><span class="pl-s1">type</span>: <span class="pl-smi">string</span><span class="pl-kos">)</span>: <span class="pl-smi">union</span> <span class="pl-kos">{</span> <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span> <span class="pl-s1">number</span><span class="pl-kos">;</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-s1">type</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-k">return</span> <span class="pl-c1">1</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>

    <span class="pl-k">case</span> <span class="pl-s">"promise"</span>:
     <span class="pl-k">return</span> <span class="pl-smi">Promise</span><span class="pl-kos">.</span><span class="pl-en">resolve</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-kos">(</span><span class="pl-c1">1</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>

    <span class="pl-k">case</span> <span class="pl-s">"date"</span>:
     <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">Date</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// static error  </span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-c">// union in invocation expression</span>
<span class="pl-k">declare</span> <span class="pl-k">function</span> <span class="pl-en">func</span><span class="pl-kos">(</span><span class="pl-s1">promiseOrValue</span>: <span class="pl-smi">union</span><span class="pl-kos"></span> <span class="pl-kos">{</span> <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span> <span class="pl-s1">number</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">)</span>: <span class="pl-k">void</span><span class="pl-kos">;</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">n</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">p</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">a</span>: <span class="pl-smi">any</span><span class="pl-kos">;</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">d</span>: <span class="pl-smi">Date</span><span class="pl-kos">;</span>

<span class="pl-en">func</span><span class="pl-kos">(</span><span class="pl-s1">n</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-en">func</span><span class="pl-kos">(</span><span class="pl-s1">p</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-en">func</span><span class="pl-kos">(</span><span class="pl-s1">a</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-en">func</span><span class="pl-kos">(</span><span class="pl-s1">d</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// static error</span></pre></div>
<h3 dir="auto">Assigning from a Type Union</h3>
<p dir="auto">When assigning to another value from a type union or type-casting from a type union, the type of the value must be compatible (as either an exact match or a subset of type information) with one of the types in the union.</p>
<p dir="auto">For example:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// assignment
declare var value: union { Promise&lt;number&gt;; number; };
var n: number;
var p: Promise&lt;number&gt;;
var a: any;
var d: Date;

n = value; // legal
p = value; // legal
a = value; // legal
d = value; // static error

// type-cast
&lt;Promise&lt;number&gt;&gt;value; // legal
&lt;number&gt;value; // legal
&lt;any&gt;value; // legal
&lt;Date&gt;value; // static error"><pre class="notranslate"><span class="pl-c">// assignment</span>
<span class="pl-k">declare</span> <span class="pl-k">var</span> <span class="pl-s1">value</span>: <span class="pl-smi">union</span><span class="pl-kos"></span> <span class="pl-kos">{</span> <span class="pl-c1">Promise</span><span class="pl-c1">&lt;</span><span class="pl-smi">number</span><span class="pl-c1">&gt;</span><span class="pl-kos">;</span> <span class="pl-s1">number</span><span class="pl-kos">;</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>
<span class="pl-k">var</span> <span class="pl-s1">n</span>: <span class="pl-smi">number</span><span class="pl-kos">;</span>
<span class="pl-k">var</span> <span class="pl-s1">p</span>: <span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-kos">;</span>
<span class="pl-k">var</span> <span class="pl-s1">a</span>: <span class="pl-smi">any</span><span class="pl-kos">;</span>
<span class="pl-k">var</span> <span class="pl-s1">d</span>: <span class="pl-smi">Date</span><span class="pl-kos">;</span>

<span class="pl-s1">n</span> <span class="pl-c1">=</span> <span class="pl-s1">value</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-s1">p</span> <span class="pl-c1">=</span> <span class="pl-s1">value</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-s1">a</span> <span class="pl-c1">=</span> <span class="pl-s1">value</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-s1">d</span> <span class="pl-c1">=</span> <span class="pl-s1">value</span><span class="pl-kos">;</span> <span class="pl-c">// static error</span>

<span class="pl-c">// type-cast</span>
<span class="pl-kos">&lt;</span><span class="pl-smi">Promise</span><span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-kos">&gt;</span><span class="pl-s1">value</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-kos">&lt;</span><span class="pl-smi">number</span><span class="pl-kos">&gt;</span><span class="pl-s1">value</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-kos">&lt;</span><span class="pl-smi">any</span><span class="pl-kos">&gt;</span><span class="pl-s1">value</span><span class="pl-kos">;</span> <span class="pl-c">// legal</span>
<span class="pl-kos">&lt;</span><span class="pl-smi">Date</span><span class="pl-kos">&gt;</span><span class="pl-s1">value</span><span class="pl-kos">;</span> <span class="pl-c">// static error</span></pre></div>