[英]TS2693: 'T' only refers to a type, but is being used as a value here
so as not to repeat the code, I would like to create an abstract class once, which will connect to the appropriate repository depending on the initialization of the class with the appropriate type T. However, I have a problem that I don't know how to call the value T and not the type as a parameter of the getRepository method from typeorm package.为了不重复代码,我想创建一个抽象 class 一次,它将连接到适当的存储库,具体取决于具有适当类型 T 的 class 的初始化。但是,我有一个我不知道的问题如何从 typeorm package 调用值 T 而不是类型作为 getRepository 方法的参数。
import {getDataSource} from "../utils/data-source";
import {Category} from "../model/category";
import {EntityTarget, ObjectLiteral} from "typeorm";
export abstract class Controller<T> {
protected repository;
public init = async () => {
this.repository = (await getDataSource()).getRepository(T);
};
}
getRepository(T);获取存储库(T); this method has the problem mentioned in the title
此方法存在标题中提到的问题
This is the easiest way to do it, if there isn't a better one如果没有更好的方法,这是最简单的方法
import {getDataSource} from "../utils/data-source";
export abstract class Controller {
protected repository;
public init = async (type) => {
this.repository = (await getDataSource()).getRepository(type);
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.