Skip to main content

Decorators

This section covers decorators used to provide binding metadata.

provide

Decorator used to set binding metadata to be loaded into the container.

warning

Like any other decorator, @provide has no effect on non loaded modules. Make sure to import the module where the decorated class is defined.

Example: providing class self binding

You can use the @provide decorator to provide a class self binding by not passing any parameters.

@injectable()
@provide()
class Katana {
public readonly damage: number = 10;
}

@injectable()
@provide()
class Ninja {
constructor(
@inject(Katana)
public readonly katana: Katana,
) {}
}

Example: providing class service binding

You can use the @provide decorator to provide a class service binding by passing the service identifier as a parameter.

@injectable()
@provide('Katana')
class Katana {
public readonly damage: number = 10;
}

@injectable()
@provide('Ninja')
class Ninja {
constructor(
@inject(Katana)
public readonly katana: Katana,
) {}
}

Example: providing class service binding with a custom binding constraint

You can use the @provide decorator to provide a class service binding with a custom binding constraint by passing the service identifier and a bind action as parameters. The bind action receives the fluent syntax to add additional configuration such as scopes and constraints.

@injectable()
@provide('Katana', (bind: BindInWhenOnFluentSyntax<Katana>) => {
bind.inSingletonScope().whenNamed('GoldenKatana');
})
class Katana {
public readonly damage: number = 10;
}

Example: providing multiple bindings

You can use the @provide decorator to provide multiple bindings by applying it multiple times.

@injectable()
@provide()
@provide('Katana')
class Katana {
public readonly damage: number = 10;
}

@injectable()
@provide()
@provide('Ninja')
class Ninja {
constructor(
@inject(Katana)
public readonly katana: Katana,
) {}
}