title: How to create types array types from unions
body: <p>This question is kind of hard to express for me which might be the reason why I did not find anything on researching.</p>
  <p>I'm trying to get a type that ends up with having arrays only. I always want to end up with &quot;one-level&quot; arrays (If arrays of arrays are given, I don't care about the output. This does not happen in my case). So the wanted outcome is the following:</p>
  <pre><code>type T1 = ForceArray&lt;string&gt; // wanted: string[]
  
  type T2 = ForceArray&lt;string[]&gt; // wanted: string[]
  </code></pre>
  <p>I can do this with the following code:</p>
  <pre><code>type ForceArray&lt;T&gt; = NonNullable&lt;T&gt; extends Array&lt;any&gt; ? T : Array&lt;T&gt;
  </code></pre>
  <p><code>NonNullable</code> is used here because in the actual use case, <code>ForceArray&lt;T&gt;</code> is used on interface properties which can be optional. This means that <code>T</code> can be <code>undefined</code>, or in other words <code>T = U | undefined</code> where <code>U</code> is the actual type (<code>U = string</code> or <code>U = string[]</code> in the shown examples).</p>
  <p>The type definition above works well, except for union types.</p>
  <pre><code>type T3 = ForceArray&lt;string | string[]&gt; // shows (string | string[])[] but I want string[]
  </code></pre>
  <p>Also other union types should be converted to arrays which does not work either:</p>
  <pre><code>type T4 = ForceArray&lt;number | string[]&gt; // wanted: number[] | string[]
  
  type T5 = ForceArray&lt;number[] | string&gt; // wanted: number[] | string[]
  </code></pre>
  <p>In the typescript docs they kind of relate to this by talking about <a href="https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types" rel="nofollow noreferrer">Distributive conditional types</a>. But as far as I understand the docs, my code should just work. So obviously I didn't understand the docs correctly.</p>
  <p>Is there a way to get the expected behaviour?</p>
  <hr />
  <p><em>Additional information</em>:</p>
  <p>The actual code that produces the output is extremely simple which is the reason why I beleave there should be a solution:</p>
  <pre class="lang-js prettyprint-override"><code>const forceArray = (value) =&gt; Array.isArray(value) ? value : [value];
  </code></pre>
  <p>This function is used in a context like this, where <code>obj</code> can implement any interface (with optional properties):</p>
  <pre class="lang-js prettyprint-override"><code>const convertObjectValuesToArray = (obj) =&gt; {
      const newObj = {};
      for (const key of obj) {
          newObj[key] = forceArray(obj[key]);
      }
      return newObj;
  }
  </code></pre>