繁体   English   中英

难以理解:如何处理与通用接口/类的绑定

[英]inversify: how to handle binding to a generic interface/class

我正在使用inversify进行使用打字稿开发的平均堆栈应用程序。 按照此URL上的说明进行操作: https ://www.npmjs.com/package/inversify,我创建了inversify.config.ts文件并添加了与我的需求相关的代码。 我的绑定之一收到以下错误:“错误:(39,71)TS2349:无法调用类型缺少调用签名的表达式。类型'typeof ExampleRepository'没有兼容的调用签名。”。

inversify.config.ts:
myContainer.bind<IExampleRepository<IGroup>>(TYPES.IExampleRepository).to(ExampleRepository<IGroup>).whenTargetNamed("exampleRepository");

types.ts:
IExampleRepository: Symbol("ExampleRepository")

inversify.config.ts条目将如何更改以适应这种需求? 我在这里做错了什么? 可以逆转处理这种情况吗?

我认为,如果您的接口是通用的IExampleRepository<T>则您的ExampleRepository不需要在其上的<IGroup>通用。

import { Container, injectable } from "inversify";


const TYPES = {
    IExampleRepository: Symbol("IExampleRepository"),
};

class IGroup {

}

interface IExampleRepository<T> {
    group: T;
}

@injectable()
class ExampleRepository implements IExampleRepository<IGroup> {
    group: IGroup
}

const myContainer = new Container();
myContainer.bind<IExampleRepository<IGroup>>(TYPES.IExampleRepository).to(ExampleRepository).whenTargetNamed("exampleRepository");

`

请提供IExampleRepository和Examplerepository的更多示例代码。 这可能有助于获得更好的答案。

暂无
暂无

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

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