title: Special-case generic type parameters for primitives
body: <h2>Background</h2>
  
  <p>I have the following generic class in TypeScript, that accepts a constructor function and a callback <em>as its constructor arguments</em>:</p>
  
  <pre><code>class InputDataPort&lt;T&gt; {
      constructor(type: new (...args: any[]) =&gt; T, listener?: (data: T) =&gt; void);
      onUpdate(listener?: (data: T) =&gt; void);
  
      // ... lots of other methods depending on the generic type parameter 'T'
  }
  </code></pre>
  
  <pre class="lang-auto prettyprint-override"><code>var dataPortA = new InputDataPort(Array, array =&gt; console.log(array)); // T: Array
  var dataPortB = new InputDataPort(Date, array =&gt; console.log(array)); // T: Date
  ...
  </code></pre>
  
  <p>Here, I'm essentially defining the generic type parameter as a constructor argument. The reason it's this way is that I need to know the generic type at runtime, and this approach enables that.</p>
  
  <h2>Problem</h2>
  
  <p>However, I'm running into a problem with primitive types. Basically, when I do the following:</p>
  
  <pre><code>var dataPortN = new InputDataPort(Number, num =&gt; console.log(num));
  </code></pre>
  
  <p><code>num</code> is of type <code>Number</code>, and the TypeScript compiler does not allow assigning a <code>Number</code> to a <code>number</code> (obviously, <code>number</code> cannot be specified as the argument to <code>type</code>):</p>
  
  <pre><code>var myNum: number;
  
  // ERROR: 'number' is a primitive, but 'Number' is a wrapper object ...
  var dataPortM = new InputDataPort(Number, num =&gt; myNum = num);
  </code></pre>
  
  <p>In this case (as well as in the case of other primitive/wrapper types), I would like to use the corresponding primitive types as the generic type parameter <code>T</code>.</p>
  
  <p><strong>Essentially, I'm looking for the following type definition:</strong></p>
  
  <ul>
  <li>if <code>T</code> is Number, then number</li>
  <li>else if <code>T</code> is String, then string</li>
  <li>else if <code>T</code> is Boolean, then boolean</li>
  <li>else <code>T</code> as-is</li>
  </ul>
  
  <p><em>To the best of my knowledge, method overloading is not sufficient here, as it doesn't provide a class-level generic type.</em></p>