optional · Core API
function optional<T>(token): OptionalDependency<T>;Marks a token as optional.
Optional dependencies resolve to undefined instead of throwing when no
provider exists in the container chain. They can be used in factory deps
or passed directly to container.get.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
|
|
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
|
|
|
Token whose provider may be absent. |
Returns
Section titled “Returns”An optional dependency descriptor resolving to T | undefined.
Example
Section titled “Example”const logger = container.get(optional(LOGGER));