I accepted Steve's answer because he suggested using Base<T extends Base<any>> , but I wanted to save a copy of the code change that fixed the problem in Stack Overflow:
class Base<T extends Base<any>> { // 1. Set as any children: Array<T>; doAction() { this.children[0].promise(); } promise(): T { return <any> this; // 2. cast to any } } class Child extends Base<Child> { public myString: string; } new Child().promise().myString;
This requires casting to anyone, but it's not so bad, since it is only in the base class. This change has no effect on the use of the Child or Base classes, so overall it was a very ideal alternative.
Update . In TS 1.7+, this can be done using polymorphic :
class Base { children: Array<this>; doAction() { this.children[0].promise(); } promise(): this { return this; } } class Child extends Base { public myString: string; } new Child().promise().myString;
David sherret
source share