0

Let's assume I use multi-injection in InversifyJS like in the official example:

container.bind<Ninja>("Ninja").to(Ninja);
container.bind<Weapon>("Weapon").to(Katana);
container.bind<Weapon>("Weapon").to(Shuriken);

Is it possible to inject a specific concretion from the bound concretions? E.g. I would like to explicitly inject the Katana concretion, how would I do this? I don't want to use multi-inject and iterate over/access the array. In my case I need the multi-injection in one place, and the single-injection in another place.

This does not work as it gives an ambiguous definition error:

@inject("Weapon") private weapon: Katana
Dominic
  • 4,572
  • 3
  • 25
  • 36

1 Answers1

0

Check out named bindings. Or tagged bindings. Also default targets.

container.bind<Weapon>("Weapon").to(Katana).whenTargetNamed("strong");
container.bind<Weapon>("Weapon").to(Shuriken).whenTargetNamed("weak");

@inject("Weapon") @named("strong") katana: Weapon,
@inject("Weapon") @named("weak") shuriken: Weapon
Ryan Dang
  • 1
  • 1