title: Function to wrap a value to array if it is not and output correct type in TypeScript
body: <p>I am trying to create a function to wrap a value to array if it is not an array</p>
  
  <pre><code>export function asArray&lt;T extends Array&lt;any&gt;&gt;(value: T): T
  export function asArray&lt;T&gt;(value: T): T[]
  export function asArray(value: any) {
    return Array.isArray(value) ? value : [value]
  }
  </code></pre>
  
  <p>But it doesn't work when the type of value is union type</p>
  
  <pre><code>type A = { a: 1 } | { a: 1 }[]
  
  const fn = (v: A) =&gt; {
    const b = asArray(v)
    return b[0].a
  }
  </code></pre>
  
  <p>it throws</p>
  
  <pre><code>Property 'a' does not exist on type 'A'.
    Property 'a' does not exist on type '{ a: 1; }[]'.ts(2339)
  </code></pre>