title: Typescript: check if a type is a union
body: <p>Is it possible to check if a given type is a union?</p>
  
  <pre><code>type IsUnion&lt;T&gt; = ???
  </code></pre>
  
  <hr>
  
  <p>Why I need this: in my code, I have the only case when some received type can be a union. I handle it with a distributive conditional type. However, it can be not obvious for one who looks at this code why a DCT is used in the first place. So I want it to be explicit like: <code>IsUnion&lt;T&gt; extends true ? T extends Foo ...</code></p>
  
  <p>I've made a few attempts with <code>UnionToIntersection</code>, with no results.  I've also come up with this one: </p>
  
  <pre><code>type IsUnion&lt;T, U extends T = T&gt; =
      T extends any ?
      (U extends T ? false : true)
      : never
  </code></pre>
  
  <p>It gives <code>false</code> for non unions, but for some reason it gives <code>boolean</code> for unions... And I have no idea why. I also tried to <code>infer</code> U from T, with no success. </p>
  
  <p>P.S. My use case may seem to someone as not perfect/correct/good, but anyway the question in the title has arised and I wonder if it's possible (I feel that it is, but am having hard time to figure it out myself).</p>