title: How to type a method to return Primitive or Instance of passed Class?
body: <p>I have a generic class with a method that should return either primitives (in case a parameter is a wrapper object to a primitive type (e.g. Number)) or an instance of the constructor passed.</p>
  <p>Thats what I tried:</p>
  <pre class="lang-js prettyprint-override"><code>/* eslint-disable @typescript-eslint/ban-types */
  type returnPrimitive&lt;T extends any&gt; = T extends String ? string
    : T extends Number ? number
    : T extends Boolean ? boolean
    : T
  /* eslint-enable */
  
  type ctor&lt;T = any&gt; = {
    new(...args: any[]): T &amp; { valueOf(): returnPrimitive&lt;T&gt;}
    [s: string]: any
  }
  
  
  class Foo&lt;T&gt; {
    Type: ctor&lt;T&gt;
    constructor (Type: ctor&lt;T&gt;) {
      this.Type = Type
    }
  
    getValue(val: any): returnPrimitive&lt;T&gt; {
      const a = new this.Type(val)
      // cast to primitive or return a
    }
  }
  </code></pre>
  <p>However, it does not really work as expected. I want it to pick up the type automatically through the type parameter in the constructor. That works well when I pass in Number or other primitive wrapper but wants to return the constructor instead of an instance for any other Class I pass in.</p>
  <pre class="lang-js prettyprint-override"><code>class Matrix{}
  new Foo(Number).getValue() // correctly returns number
  new Foo(Matrix).getValue() // returns typeof Matrix instead of Matrix
  </code></pre>
  <p>How can I fix this?</p>