Skip to content
GitHub

Inject

function in meocord/decorator Since 4.1.0

Inject(
  token: ProviderToken,
): (
  target: object,
  propertyKey: string | symbol | undefined,
  parameterIndex: number,
) => void

Injects what a token provides into a constructor parameter.

Use it for a value provided under a string, a symbol or a createToken token, such as a database pool made by a factory, or to inject a different class than the parameter's type. A parameter typed as a class is injected by its type, and needs no decorator.

Parameters

NameTypeDescription
tokenProviderToken

The token a provider in @MeoCord({ providers }) is bound under.

Returns

Returns a parameter decorator.

(
  target: object,
  propertyKey: string | symbol | undefined,
  parameterIndex: number,
) => void

Examples

TypeScript
const GREETING = createToken<string>('GREETING')

@Service()
export class Greeter {
  constructor(@Inject(GREETING) private readonly greeting: string) {}
}