title: TypeScript: get typeof generic constructor
body: <p>In TypeScript, I've defined a helper function called <code>create</code> that takes a constructor and the constructor's typed arguments to create a new instance of a class: </p>
  
  <pre class="lang-js prettyprint-override"><code>function create&lt;Ctor extends new (...args: any[]) =&gt; any, R extends InstanceType&lt;Ctor&gt;&gt;(
    ctor: Ctor,
    ...args: ConstructorParameters&lt;Ctor&gt;
  ): R {
    return new ctor(...args)
  }
  </code></pre>
  
  <p>This works for simple classes like so:</p>
  
  <pre class="lang-js prettyprint-override"><code>class Bar  {
    constructor(param: number) { }
  }
  
  const bar1 = create&lt;typeof Bar, Bar&gt;(Bar, 1);
  
  // Compile error: Argument of type '"string"' is not assignable to parameter of type 'number'.ts(2345)
  const bar2 = create&lt;typeof Bar, Bar&gt;(Bar, 'string');
  </code></pre>
  
  <p>However, if I have a generic class, I can't get TypeScript to perform the proper type checking on <code>create</code>:</p>
  
  <pre class="lang-js prettyprint-override"><code>class Foo&lt;T&gt;  {
    constructor(param: T) { }
  }
  
  // Ok
  const foo1 = create&lt;typeof Foo, Foo&lt;number&gt;&gt;(Foo, 1);
  
  // This should be an error but is not
  const foo2 = create&lt;typeof Foo, Foo&lt;number&gt;&gt;(Foo, { not: 'a number' })
  </code></pre>
  
  <p>The root cause is that signature of <code>create</code> in the second case takes an <code>unknown</code> parameter:</p>
  
  <p><a href="https://i.stack.imgur.com/3hysp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3hysp.png" alt="enter image description here"></a></p>
  
  <h2>Main question</h2>
  
  <p>Is it possible to explicitly write out the type of the generic class constructor in the call to <code>create</code> without inlining the type itself and while still preserving the signature of <code>create</code>?</p>
  
  <h2>Attempts</h2>
  
  <p>My first thought was:</p>
  
  <pre class="lang-js prettyprint-override"><code>create&lt;typeof Foo&lt;number&gt;, Foo&lt;number&gt;&gt;(Foo, { not: 'a number' })
  </code></pre>
  
  <p>But that is invalid code. </p>
  
  <p>So far, the best workaround I've found is to use a temporary class to explicitly capture the constructor type:</p>
  
  <pre class="lang-js prettyprint-override"><code>class Temp extends Foo&lt;number&gt; { }
  
  // This correctly generates an error
  create&lt;typeof Temp, Foo&lt;number&gt;&gt;(Foo, { not: 'a number' });
  </code></pre>
  
  <p>Can this be done in a more elegant way?</p>