title: Can Typescript infer the type of an instance of an extension class instantiated by a method of its base?
body: <p>Consider the following Typescript snippet:</p>
  
  <pre><code>class Animal {
    constructor(name: string) {
      this.name = name;
    }
    name: string;
  
    haveBaby(name: string): ?? return type ?? {
      return new this.constructor(name); // Error
    }
  }
  
  class Cat extends Animal {}
  class Dog extends Animal {}
  class Gerbil extends Animal {} // etc.
  
  let sorachi = new Cat("Sorachi"); // a: Cat
  let sorachiJr = a.haveBaby("Sorachi Jr."); // I want: sorachiJr: Cat
  </code></pre>
  
  <p>Animals can have babies, and a baby should be the same kind of animal as the parent, i.e., should be an instance of the same class as the parent. How do I assign types in this situation, so that Typescript knows that <code>sorachiJr: Cat</code>?</p>
  
  <p><em>The code snippet above doesn't work.</em> The line <code>return new this.constructor(name)</code> produces the error <code>[ts] Cannot use 'new' with an expression whose type lacks a call or construct signature.</code> in VS Code. The only solution I was able to find and understand was replacing <code>this.constructor(name)</code> with <code>(&lt;any&gt;this.constructor)(name)</code> or <code>(&lt;any&gt;this).constructor(name)</code>, but then the type inferred for <code>sorachiJr</code> is <code>any</code>, too, rather than <code>Cat</code>.  I tried casting to <code>typeof this</code> rather than <code>any</code>, but got the error <code>[ts] Cannot find name 'this'</code>.</p>
  
  <p>How can I convince Typescript that having babies is a species-preserving operation?</p>