简体   繁体   English

TS2693:“T”仅指类型,但在此处用作值

[英]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.

相关问题 打字稿:错误 TS2693:“Promise”仅指一种类型,但在此处用作值 - typescript: error TS2693: 'Promise' only refers to a type, but is being used as a value here 'Set'仅指类型,但在此处用作值。 (TS2693) - 'Set' only refers to a type, but is being used as a value here. (TS2693) 错误TS2693:“地图”仅引用类型,但在此处被用作值 - error TS2693: 'Map' only refers to a type, but is being used as a value here Typescript:错误 TS2693:“注意”仅指一种类型,但在此处用作值 - Typescript: error TS2693: 'Note' only refers to a type, but is being used as a value here 'Employee' 仅指类型,但在此处用作值。ts(2693) - 'Employee' only refers to a type, but is being used as a value here.ts(2693) &#39;ObjectId&#39; 仅指一种类型,但在此处用作值。ts(2693) - 'ObjectId' only refers to a type, but is being used as a value here.ts(2693) io-ts:仅指一个类型,但在此处用作值 - io-ts: only refers to a type, but is being used as a value here 仅引用类型,但在此处用作值 - only refers to a type, but is being used as a value here TS:模块扩充, <Type> 仅引用类型,但在此处用作值 - TS: Module augmentation, <Type> only refers to a type but is being used as a value here TS2585:“Promise”仅指类型,但在此处用作值 - TS2585: 'Promise' only refers to a type, but is being used as a value here
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM