Skip to content

Function: inject()

Call Signature

ts
function inject<T>(token: ServiceToken<T>): T;

Defined in: src/wirestate-core/container/container-context.ts:37

Resolves a dependency from the container currently constructing a service.

Type Parameters

Type ParameterDescription
TValue type resolved for the token.

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to resolve from the current container.

Returns

T

The resolved value.

Remarks

Use inject() in constructor defaults or field initializers of @Injectable() classes, or inside factory bindings. Dependency resolution uses the same rules as Container.get(), including parent lookup.

Throws

WirestateError If the token is not bound, or if a circular dependency is detected while constructing the value. Errors thrown by a binding's constructor or factory propagate unchanged.

Throws

Error If there is no active injection context.

Call Signature

ts
function inject<T>(token: ServiceToken<T>, options: {
  optional: true;
}): Optional<T>;

Defined in: src/wirestate-core/container/container-context.ts:49

Optionally resolves a dependency from the current injection context.

Type Parameters

Type ParameterDescription
TValue type resolved for the token.

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to resolve from the current container.
options{ optional: true; }Optional lookup options.
options.optionaltrueReturn undefined instead of throwing on a miss.

Returns

Optional<T>

The resolved value, or undefined when the token is not bound.

Call Signature

ts
function inject<T>(token: ServiceToken<T>, options: {
  lazy: true;
}): () => T;

Defined in: src/wirestate-core/container/container-context.ts:68

Returns a lazy resolver for a dependency in the current injection context.

Type Parameters

Type ParameterDescription
TValue type resolved for the token.

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to resolve from the current container.
options{ lazy: true; }Lazy lookup options.
options.lazytrueReturn a resolver function instead of resolving immediately.

Returns

Function that resolves the token when called.

() => T

Remarks

The returned function closes over the active container and resolves the token when called. Use it to break circular dependencies or avoid constructing a dependency until a method needs it.

Throws

Error If there is no active injection context.

Call Signature

ts
function inject<T>(token: ServiceToken<T>, options: {
  lazy: true;
  optional: true;
}): () => Optional<T>;

Defined in: src/wirestate-core/container/container-context.ts:81

Returns a lazy optional resolver for a dependency in the current injection context.

Type Parameters

Type ParameterDescription
TValue type resolved for the token.

Parameters

ParameterTypeDescription
tokenServiceToken<T>Token to resolve from the current container.
options{ lazy: true; optional: true; }Lazy optional lookup options.
options.lazytrueReturn a resolver function instead of resolving immediately.
options.optionaltrueReturn undefined from the resolver on a miss.

Returns

Function that resolves the token when called, or returns undefined when missing.

() => Optional<T>