【问题标题】:Sequelize-Typescript typeof modelSequelize-Typescript typeof 模型
【发布时间】:2019-08-05 13:36:21
【问题描述】:

我正在尝试创建一个基本的 crud 服务,该服务采用 Sequelize 模型并为其创建所有基本 API,所以我这样做了:

export class RepositoryService<T extends Model<T>> {
  constructor(protected model: typeof Model) {
  }
  public async getMany(
    query: RequestParamsParsed = {},
    options: RestfulOptions = {},
  ): Promise<T[]> {
    return this.model.findAll();
  }
}

我收到以下错误:

The 'this' context of type 'typeof Model' is not assignable to method's 'this' of type 'new () => Model<Model<any>>'.
  Cannot assign an abstract constructor type to a non-abstract constructor type.

这是因为 seqeulize-typescript 包中的这一行:

static findAll<T extends Model<T>>(this: (new () => T), options?: IFindOptions<T>): Promise<T[]>;

我对@9​​87654325@ 比较陌生,所以如果有人能告诉我this: (new () =&gt; T)findAll 函数中的含义是什么,我该如何解决。

【问题讨论】:

  • arg: (new () =&gt; T) 表示 arg 不是 T 类型的实例,就像在 (arg: T) 中一样。 arg 是 T 类。您也可以对 (arg: { new (...args): T }) 使用此语法

标签: typescript sequelize-typescript


【解决方案1】:

在使用 sequelize-typescript 库时也遇到此错误。

你可以先定义这些辅助类型:

import { Model } from "sequelize-typescript";

// Type `ModelType` would basically wrap & satisfy the 'this' context of any sequelize helper methods
type Constructor<T> = new (...args: any[]) => T;
type ModelType<T extends Model<T>> = Constructor<T> & typeof Model;

然后,将它与您的代码一起使用:

export class RepositoryService<T extends Model<T>> {
  constructor(protected model: ModelType<T>) {}

  public async getMany(
    query: RequestParamsParsed = {},
    options: RestfulOptions = {},
  ): Promise<T[]> {
    return this.model.findAll();
  }
}

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    问题似乎出在这一行

    constructor(protected model: typeof Model) {}
    

    我将模型类型设置为的 Model 是从 sequelize-typescript 导入的,而我应该使用从 sequelize 本身导出的原始模型。

    整个代码是这样的:

    import { Model as OriginalModel } from 'sequelize';
    import { Model } from 'sequelize-typescript';
    export class RepositoryService<T extends Model<T>> {
      constructor(protected model: typeof OriginalModel) {
      }
      public async getMany(
        query: RequestParamsParsed = {},
        options: RestfulOptions = {},
      ): Promise<T[]> {
        return this.model.findAll();
      }
    }
    

    【讨论】:

    • 下面@Adrian 提供的更好解决方案。
    【解决方案3】:

    使用"sequelize-typescript": "2.1.0",您可以使用模块提供的ModelCtor,例如。

    import { Model, ModelCtor } from 'sequelize-typescript';
    
    export class RepositoryService<T extends Model<T>> {
      constructor(protected model: ModelCtor<T>) {}
    
      public async getMany(
        query: RequestParamsParsed = {}, 
        options: RestfulOptions = {}
    ): Promise<T[]> {
        return this.model.findAll();
      }
    }
    

    ModelCtor 类型是:

    export declare type Repository<M> = (new () => M) & NonAbstract<typeof Model>;
    
    export declare type ModelCtor<M extends Model = Model> = Repository<M>;
    
    

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 2020-09-09
      • 2019-02-04
      • 2020-08-19
      相关资源
      最近更新 更多