使用打字稿

时间:2019-09-24 17:10:10

标签: typescript sequelize.js extends typescript-generics sequelize-typescript

我正在尝试创建Sequelize Model类的存根,以便能够执行依赖注入以进行单元测试。但是,我很难找到合适的模板参数来使其正常工作。

到目前为止,我有以下内容:

class ModelStub extends Model {
    public static findAll<M extends Model>(this: (new () => M) & typeof Model, options?: FindOptions): Bluebird<M[]> {
        return super.findAll.call(this, options);
    }
}

这会给我以下错误消息:

error TS2417: Class static side 'typeof ModelStub' incorrectly extends base class static side 'typeof Model'. 
  Types of property 'findAll' are incompatible.
    Type '<M extends import("C:/Users/CMalek/Desktop/Web-Server/node_modules/sequelize/types/lib/model").Model<any, any>>(this: (new () => M) & typeof import("C:/Users/CMalek/Desktop/Web-Server/node_modules/sequelize/types/lib/model").Model, options?: import("C:/Users/CMalek/Desktop/Web-Server/node_modules/sequelize/types/lib...' is not assignable to type '<M extends import("C:/Users/CMalek/Desktop/Web-Server/node_modules/sequelize/types/lib/model").Model<any, any>>(this: (new () => M) & typeof import("C:/Users/CMalek/Desktop/Web-Server/node_modules/sequelize/types/lib/model").Model, options?: import("C:/Users/CMalek/Desktop/Web-Server/node_modules/sequelize/types/lib...'. Two different types with this name exist, but they are unrelated.
      Type 'Bluebird<Model<unknown, unknown>[]>' is not assignable to type 'Bluebird<M[]>'.
        Types of property 'then' are incompatible.
          Type '{ <U>(onFulfill?: (value: Model<unknown, unknown>[]) => Resolvable<U>, onReject?: (error: any) => Resolvable<U>): Bluebird<U>; <TResult1 = Model<unknown, unknown>[], TResult2 = never>(onfulfilled?: (value: Model<unknown, unknown>[]) => Resolvable<TResult1>, onrejected?: (reason: any) => Resolvable<...>): Bluebird<.....' is not assignable to type '{ <U>(onFulfill?: (value: M[]) => Resolvable<U>, onReject?: (error: any) => Resolvable<U>): Bluebird<U>; <TResult1 = M[], TResult2 = never>(onfulfilled?: (value: M[]) => Resolvable<TResult1>, onrejected?: (reason: any) => Resolvable<...>): Bluebird<...>; }'.
            Types of parameters 'onFulfill' and 'onFulfill' are incompatible.
              Types of parameters 'value' and 'value' are incompatible.
                Type 'Model<unknown, unknown>[]' is not assignable to type 'M[]'.
                  Type 'Model<unknown, unknown>' is not assignable to type 'M'.
                    'Model<unknown, unknown>' is assignable to the constraint of type 'M', but 'M' could be instantiated with a different subtype of constraint 'Model<any, any>'.

我是Typescript的新手,所以不确定如何在Model<unknown, unknown> and Model<any, any>之间建立桥梁?

在最坏的情况下,我当然可以创建自己的类来包装Sequelize并为其创建一个存根,但是我宁愿避免添加额外的代码层。

任何帮助将不胜感激。

更新

我能够提出一个可以与函数重载一起使用的签名,但这并不漂亮。我希望有简单得多的东西。这是重载的版本:

export default class ModelStub extends Model {
  public static sFindAllFindOptions: FindOptions;
  public static findAll(this: (new () => ModelStub) & typeof Model, options?: FindOptions): Bluebird<ModelStub[]>;
  public static findAll<M extends Model>(this: (new () => M) & typeof Model, options?: FindOptions): ReturnType<typeof Model.findAll>;
  public static findAll<M extends Model>(this: (((new () => ModelStub) & typeof Model) | ((new () => M) & typeof Model)), options?: FindOptions): Bluebird<ModelStub[]>|ReturnType<typeof Model.findAll> {
    ModelStub.sFindAllFindOptions = options;
    return super.findAll.call(this, options);
  }
}

我怀疑这部分是由于ReturnType函数的类型推断。

0 个答案:

没有答案