Named bindings

We can use named bindings to fix AMBIGUOUS_MATCH errors when two or more concretions have been bound to the an abstraction. Notice how the constructor arguments of the Ninja class have been annotated using the @named decorator:

interface Weapon {}

class Katana implements Weapon {}

class Shuriken implements Weapon {}

interface Ninja {
    katana: Weapon;
    shuriken: Weapon;

class Ninja implements Ninja {
    public katana: Weapon;
    public shuriken: Weapon;
    public constructor(
        @inject("Weapon") @named("strong") katana: Weapon,
        @inject("Weapon") @named("weak") shuriken: Weapon
    ) {
        this.katana = katana;
        this.shuriken = shuriken;

We are binding Katana and Shuriken to Weapon but a whenTargetNamed constraint is added to avoid AMBIGUOUS_MATCH errors:


results matching ""

    No results matching ""