简体   繁体   中英

inversify: how to handle binding to a generic interface/class

I am using inversify for an mean stack application developed with typescript. Following the instructions here at this url: https://www.npmjs.com/package/inversify , I created the inversify.config.ts file and added the code relevant to my needs. I am receiving the following error for one of my binding: "Error:(39, 71) TS2349:Cannot invoke an expression whose type lacks a call signature. Type 'typeof ExampleRepository' has no compatible call signatures.".

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

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

How would the inversify.config.ts entry have to change to accomodate this need? What am I doing wrong here? Can inversify handle this scenario?

I think that if your interface is generic IExampleRepository<T> then your ExampleRepository doesn't need the <IGroup> generic on it.

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");

`

Please provide more example code for IExampleRepository and Examplerepository. That might help get a better answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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