title: TypeScript generic class that creates instances of its type variable's class?
body: <p>I'm familiar with generic classes in TypeScript, where a class can be defined with an associated type variable, and then instances with a specific type can manipulate and return values of that type.</p>
  
  <p>Problem: I want a generic class that <em>creates</em> instances of the type variable. For instance:</p>
  
  <pre><code>class Thing {
    thingProp: string;
  }
  
  class ThingOne extends Thing {
    thingOneProp: string;
  }
  
  class ThingTwo extends Thing {
    thingTwoProp: string;
  }
  
  class Maker&lt;T extends Thing&gt; {
  
    make(): T {
      return new T();
      //         ^--- " // &lt;- "error TS2304: Cannot find name 'T'""
    }
  
  }
  
  let thingOneMaker = new Maker&lt;ThingOne&gt;();
  
  let thingOne: ThingOne = thingOneMaker.make();
  
  let thingTwoMaker = new Maker&lt;ThingTwo&gt;();
  
  let thingTwo: ThingTwo = thingTwoMaker.make();
  
  let thingError: ThingOne = thingTwoMaker.make();
  //      ^--- "error TS2322: Type 'ThingTwo' is not assignable to type 'ThingOne'"
  </code></pre>
  
  <p>This almost seems to work. The compiler generates code, and the error on the last line shows that TypeScript understands what type <code>thingTwoMaker.make()</code> should return.</p>
  
  <p>However, the error on <code>return new T();</code> shows that TypeScript doesn't understand that I'm trying to make instances of the type variable's class, and the generated JavaScript confirms it:</p>
  
  <pre><code>var Maker = (function () {
      function Maker() {
      }
      Maker.prototype.make = function () {
          return new T(); // &lt;- "error TS2304: Cannot find name 'T'"
      };
      return Maker;
  }());
  </code></pre>
  
  <p>And, not surprisingly, running the generated JavaScript with Node.js produces a <code>ReferenceError: T is not defined</code> error.</p>
  
  <p>How can I make a generic class whose instances can <em>create</em> instances of the type variable class? (Tested using TypeScript 2.0.10.)</p>