我希望能够基于通用类型初始化一个TypeORM仓库。
例如:
例如:
import { Connection, Repository } from 'typeorm';
export class GenericService<T> {
private repository: Repository<T>;
constructor(connection: Connection) {
this.repository = connection.getRepository(T);
// 'T' only refers to a type, but is being used as a value here.ts(2693)
}
public async list(): Promise<T[]> {
return await this.repository.find();
}
}
但是我无法将泛型类型传递给ORM存储库工厂。
'T'只是一个类型的引用,但在此处被用作值。ts(2693)
我要如何基于泛型类型创建此通用服务?
附:我在C#中确切地做到了这一点,它像魔法一样起作用,节省了我很多时间。
T
,因此当然不能将其用作参数。Yeysides提供了一个解决方法。 - Mu-Tsun Tsai