title: TypeScript - Constraint for array
body: <p>I have the following (example-)array:</p>
  
  <pre><code>[
    'test-string', 
    foo =&gt; ({ foo }), 
    'other-string', 
    bar =&gt; ({ bar })
  ]
  </code></pre>
  
  <p>with the (example-)interface</p>
  
  <pre><code>interface MyValues { foo: string; bar: string; }
  </code></pre>
  
  <p>A function that expects this array type must ensure that the unification of the results of all functions implements the complete interface. Is that possible?
  Currently, I have:</p>
  
  <pre><code>type Fn&lt;Values&gt; = (part: string) =&gt; { [key in keyof Values]?: string }
  type Item&lt;Values&gt; = string | Fn&lt;Values&gt;;
  
  function extract&lt;Values&gt;(items: Item&lt;Values&gt;[]) {
    const values = {} as Values;
    // (...)
    return values;
  }
  </code></pre>
  
  <p>However, this typing only checks that all functions return objects that match the keys of <code>Values</code> and not that all keys are finally present.</p>
  
  <p>I am not quite sure if this check is even possible with TypeScript, I have found <a href="https://stackoverflow.com/questions/40510611/typescript-interface-require-one-of-two-properties-to-exist/49725198#49725198">this answer</a> which also goes towards "calculations" of types, but I am not sure if this is applicable for this use-case.</p>