export class Base {
static getSomething():typeof this //<-- change this?
{
var type = typeof this;
return new this['type']();
}
}
export class Foo extends Base {
publicVar:string = 'getThis!';
}
var foo:Foo = Foo.getSomething();
var success:string = foo.publicVar;
上面的返回錯誤是因爲編譯器說Foo.getSomething會返回一個Base而不是Foo。 我想知道是否有另一種方式讓我調用編譯器知道的Foo的靜態函數將返回Foo。是否有可能在打字稿中有函數返回延伸它的類的類型
Ofcourse我可以明確地Foo中的每一個擴展的基類實現它,因此,我也可以簡單地強制轉換它:
var foo:Foo = <Foo> Foo.getSomething();
但我不知道是否有無需做任何一種方式這些事情,因爲我將使用這種方法很多