[英]In Typescript, how can I make a generic service that can call static methods on a model defined by a type param
I want to do something like this:我想做这样的事情:
import {Company} from 'company.model.ts'
@Service()
export class CompanyService extends BaseService<Company> {
constructor() {
super()
this.Model = Company
}
}
@Service()
export class BaseService<T extends typeof Model> {
Model: typeof T
async findAll(input) {
return await this.Model.create(input)
}
}
I want to call static methods on a class with a type of typeof T
.我想在类型为
typeof T
的类上调用静态方法。
Also, is there a way to avoid having to assign a typeof T
to an instance variable?另外,有没有办法避免将
typeof T
分配给实例变量?
Solution is to use:解决方法是使用:
Model: {new(): T} & typeof Sequelize.Model
Thanks to: https://github.com/RobinBuschmann/sequelize-typescript/issues/231#issuecomment-471724824感谢: https : //github.com/RobinBuschmann/sequelize-typescript/issues/231#issuecomment-471724824
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.