typescript ignore无法实例化抽象类错误

时间:2017-12-05 20:12:27

标签: typescript

TS编译器抱怨我无法在return new this

实例化MeteorCollection
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本身。

1 个答案:

答案 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);
  }
}