title: TypeScript Generic Factory Function Type, matching array element order
body: <p>I want to build some kind of <code>FactoryFactory</code>: Basically a generic function that returns a factory function. Writing the function itself is simple, but I can't figure out how to do the TypeScript typings for it.</p>
  <p>The function should be used like this:</p>
  <pre><code>const stubFactoryFunction = (...props) =&gt; (...values) =&gt; ({ /* ... */ });
  
  const factory = stubFactoryFunction(&quot;prop1&quot;, &quot;prop2&quot;);
  const instance = factory(&quot;foo&quot;, 42);
  console.log(instance); // { prop1: &quot;foo&quot;, prop2: 42 }
  </code></pre>
  <p>At first I tried to provide the value types as an array:</p>
  <pre><code>type FactoryFunction&lt;T extends any[]&gt; =
    &lt;K extends string[]&gt;(...props: K) =&gt;
      (...values: T[number]) =&gt;
        {[key in K[number]]: T[number]}
  </code></pre>
  <p>But this will result in <code>{ prop1: string | number, prop2: string | number}</code>, because the type doesn't match the array indexes.</p>
  <p>Next I tried to provide the whole object as generic type:</p>
  <pre><code>type FactoryFunction&lt;T extends {[key: string]: any}&gt; =
    (...props: (keyof T)[]) =&gt;
      (...values: ???) =&gt;
        T
  </code></pre>
  <p>And here I got a similar problem: <code>values</code> must somehow match the order of <code>props</code>.</p>
  <p>Is this possible at all?</p>
  <p><strong>Bonus 1:</strong> Don't allow duplicate props.</p>
  <p><strong>Bonus 2:</strong> Enforce the provide all non-optional props from <code>T</code>.</p>