【发布时间】:2017-01-26 05:01:04
【问题描述】:
我知道打字稿supports return type this。这可能是我需要的,除了我有一个静态方法,它返回定义它的类的实例或它的子类。所以我尝试使用:
class A {
static createInstance(resource):Promise<this> //<---what can I put here??
{
//pseudo code: load a module, async
return loadModule(resource.module).then(() => {
//find Class from resource data, can return A, or B or any subclass
var targetClass = resource.getType().getClass();
//create instance of A or any subclass of A
return new targetClass.prototype.constructor();
}
}
}
//module B.js which is loaded on demand:
class B extends A {
//I dont want to have to overwrite the returntype of createInstance
//in every subclass of A
}
Promise<this> 不起作用。 Promise<typeof this> 也没有。有没有其他方法可以做我想做的事?
【问题讨论】:
-
您介意提供一些来自周围代码的上下文吗?通常这样的方法是静态的。
-
是的,你是对的,我实际使用的方法是静态的。我用我正在尝试做的事情更新了代码
标签: typescript types this