0

I want to be able to simply specify what is needed without adding strings or symbols. This seems to work for declaring the binding:

container.bind<Weapon>(Shuriken);

But I get a run-time error if I don't use @inject and don't know what to put in it when it's being injected:

public constructor(
    @inject() weapon: Weapon // compile-time error
) {
Jason Goemaat
  • 28,692
  • 15
  • 86
  • 113

1 Answers1

-1

When you use an interface (I assume that Weapon is an interface) you need to ensure that the same ID is used when you declare the binding:

container.bind<Weapon>(Shuriken).toSelf();

And then you declare the injection:

@injectable()
class SomeClass {
    public constructor(
        @inject(Shuriken) weapon: Weapon; // Use the right ID!
    ) {

However, using the class removes the benefits of dependency injection. If you want you to use classes you could actually do something more simple:

@injectable()
class SomeClass {
    public constructor(
        weapon: Shuriken; // Use the class as Type!
    ) {

The recommended solution is to use strings or symbols as IDs:

const TYPE = { Weapon: "Weapon" };

container.bind<Weapon>(TYPE.Weapon).to(Shuriken);

@injectable()
class SomeClass {
    public constructor(
        @inject(TYPE.Weapon) weapon: Weapon;
    ) {
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93