【发布时间】: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[]>;
我对@987654325@ 比较陌生,所以如果有人能告诉我this: (new () => T) 在findAll 函数中的含义是什么,我该如何解决。
【问题讨论】:
-
arg: (new () => T)表示 arg 不是T类型的实例,就像在(arg: T)中一样。 arg 是 T 类。您也可以对(arg: { new (...args): T })使用此语法
标签: typescript sequelize-typescript