title: Constraining a generic parameter to be a union type in Typescript
body: <p>Is there a way in Typescript to constrain a generic parameter to be a union type? To illustrate my intention, below I pretend <code>T extends UnionType</code> will achieve this:</p>
  <pre class="lang-js prettyprint-override"><code>function doSomethingWithUnion&lt;T extends UnionType&gt;(val: T) {}
  
  doSomethingWithUnion&lt;number | string&gt;(...) // good
  doSomethingWithUnion&lt;number&gt;(...) // should result in an error
  </code></pre>
  <p>I've found in another SO question  (<a href="https://stackoverflow.com/questions/53953814/typescript-check-if-a-type-is-a-union">53953814</a>) that we can check if a type is a union type:</p>
  <pre class="lang-js prettyprint-override"><code>type IsUnion&lt;T&gt; = [T] extends [UnionToIntersection&lt;T&gt;] ? false : true
  
  type Foo = IsUnion&lt;string | number&gt; // true
  type Bar = IsUnion&lt;string&gt; // false
  </code></pre>
  <p>Which allows to, for example, to assert than a function will never return if a generic parameter is <em>not</em> a union type:</p>
  <pre><code>declare function doSomethingWithUnion&lt;T&gt;(val: T): [T] extends [UnionToIntersection&lt;T&gt;] ? never: boolean;
  
  doSomethingWithUnion&lt;number&gt;(2) // never
  doSomethingWithUnion&lt;string|number&gt;(2) // =&gt; boolean
  </code></pre>
  <p>(<a href="https://www.typescriptlang.org/play?ssl=14&amp;ssc=53&amp;pln=11&amp;pc=1#code/C4TwDgpgBAqgdgSwPZwCpIJJ2BATgZwgGNhk4AeGAPigF4AoKKAChiggA8c4ATfKAIZwQUAPwsA1gC5YASjo0AbkgQ8oMuBEV55nbnxbNpUBHABmeKBnm0lKnvPEZ1UTdtz16oSFfzwy5Kg0tFAA2qgAuuxcELz8of4o6Fg4BMSkKIFUUeJmAgA2hC7AuACuEJ5e4NAAYkhIdL6JFPglpgDmUAA+rqUAtgBGeDQA9CNQJeVVPgBCAriNGH6Ima24HaPjeYUVnjzE+fPQZqVwJGRQPEgAykh9EMAAFh0A6ghPzVnMigUyqLIyMKRaL6eLNZLYPCEc6ZII5VxaPAyAb1fIQIQAbj2NzuD2ecHabw+KwocH6Q1wVGYACZ5GMEe56Fdbvcnq93o9PmsOl0yYNhjS6eNbFAUUg0UJ6EA" rel="nofollow noreferrer">Link to playground</a>)</p>
  <p>However, I have not found a way to constrain the type itself in a similar manner.</p>