TS编译器抱怨我无法在return new this
行
abstract class MeteorCollection {
static $collection: Collection<any>
static findOne(selector: Selector = {}, options: FindOneOptions = {}) {
let item = this.$collection.findOne(selector, options)
return new this(item)
}
}
我怎么能告诉它它没关系,因为它指的是一个类的构造函数,它将扩展MeteorCollection而不是MeteorCollection本身。
答案 0 :(得分:1)
无需忽略错误。我假设你打算从引用到子类调用方法,所以使用this
类型的注释。
abstract class MeteorCollection {
static $collection: Collection<any>;
static findOne<T extends MeteorCollection>(
this: typeof MeteorCollection & (new (arg: any, ...args: any[]) => T),
selector: Selector = {},
options: FindOneOptions = {}
) {
const item = this.$collection.findOne(selector, options);
return new this(item);
}
}