InversifyJS : can not bind using interface

2.5k views Asked by At

I am trying to use inversify JS to inject dependencies on TypeScript App. I started by using the example of https://github.com/inversify/InversifyJS page :

// file interfaces.ts
interface Warrior {
    fight(): string;
}

// file types.ts
const TYPES = {
    Warrior: Symbol("Warrior")
};
export { TYPES };

// file entities.ts
import { injectable, inject } from "inversify";
import "reflect-metadata";
import { Warrior } from "./interfaces"
import { TYPES } from "./types";

@injectable()
class WarriorImpl implements Warrior {
    public constructor(){
    }
    public fight() { return "I fight"; }
}
export { WarriorImpl };

// file inversify.config.ts
import { Container } from "inversify";
import TYPES from "./types";
import { Warrior } from "./interfaces";
import { WarriorImpl } from "./entities";
const myContainer = new Container();
myContainer.bind<Warrior>(TYPES.Warrior).to(WarriorImpl);
export { myContainer };

I applied what is provided in the example but Vscode and tsc failed at the binding line by showing this error [ts] Untyped function calls may not accept type arguments. [ts] Cannot find name 'Warrior'.

2

There are 2 answers

0
Oussama BEN MAHMOUD On BEST ANSWER

Your example is clean and correct all you have to do is to upgrade your typescript version in your package.json

"devDependencies": {
   "typescript": "3.9.3"
}
3
Saravana On
  1. You have to export the interface for it to be imported:

    // file interfaces.ts
    export interface Warrior {
        fight(): string;
    }
    
  2. Your import in inversify.config.ts should be:

    import { TYPES } from "./types";
    
  3. Make sure you have actually installed the library (npm install inversify). The error Untyped function calls may not accept type arguments is sue to missing type information.