如果B扩展A,A可以定义一个创建新B的方法吗?
progress_attachments
答案 0 :(得分:5)
你走了:
type SetConstructor<T extends SetA> = {
new (items:any[]): T;
}
class SetA {
constructor(public items:any[]) {}
createNew(items): this {
return new (this.constructor as SetConstructor<this>)(items);
}
clone(){
return this.createNew(this.items);
}
}
createNew
方法的返回类型为this
Polymorphic this types。