简体   繁体   English

在 Typescript 中,如何创建可以在由类型参数定义的模型上调用静态方法的通用服务

[英]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分配给实例变量?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM