Documents injecute 1.x · Migrating from 0.x
Skip to content

Function: optional() ​

optional<K>(key): OptionalDependency<K>

Defined in: src/dependencies.ts:28

Marks a dependency as optional: the factory receives the service when it is registered, and undefined otherwise.

Type Parameters ​

K ​

K extends ServiceKey

Parameters ​

key ​

K

Returns ​

OptionalDependency<K>

Example ​

ts
container.addSingleton(
  'mailer',
  (transport) => new Mailer(transport ?? consoleTransport),
  [optional('transport')],
);

Released under the MIT License.