您可以使用 的类型prototype而不是构造函数返回的类型。
type AbstractInstanceType<T> = T extends { prototype: infer U } ?
U : never
abstract class Foo{
foo:number
}
declare var foo: AbstractInstanceType<typeof Foo>;
foo.foo //ok
Run Code Online (Sandbox Code Playgroud)
使用构造函数签名可能会更好,而您本来会使用typeof AbstractClass. InstanceType将在这方面工作:new (... a:any[]) => AbstractClass
如果要在抽象类上创建静态工厂方法,这将起作用:
abstract class Foo{
foo: number
static create<T extends Foo>(this: new () => T) {
return new this();
}
static createWithArgs<T extends Foo, A extends any[]>(this: new (...a: A) => T, ...a: A) {
return new this(...a);
}
}
class Bar extends Foo { }
class Baz extends Foo {
constructor(public n: string) {
super();
}
}
Bar.create()
Baz.createWithArgs("")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
213 次 |
| 最近记录: |