title: TypeScript clone self instance with self Class Type
body: <p>I want to clone the current class instance and create inside <code>clone()</code> an instance of a polymorphic class, something like this:</p>
  
  <pre><code>class State
  {
      public clone():State
      {
          const state = new State();
          this._copyData(state);
          return state;
      }
  
      protected _copyData(target:this):void
      {
      }
  }
  
  class StateExtends extends State
  {
      public clone():StateExtends
      {
          const state = new StateExtends();
          return state;
      }
  
      protected _copyData(target:this):void
      {
          super._copyData(target);
      }
  }
  </code></pre>
  
  <p>When overriding the State class I want the <code>clone()</code> signature to stay the same in all classes hierarchy. Can I do something like this:</p>
  
  <pre><code>class State
  {
      public clone():this
      {
          const state = new this();
          this._copyData(state);
          return state;
      }
  
      protected _copyData(target:this):void
      {
      }
  }
  
  class StateExtends extends State
  {
      protected _copyData(target:this):void
      {
          super._copyData(target);
      }
  }
  </code></pre>
  
  <p>But this is not working. </p>
  
  <p>Any other suggestions?</p>