Function ConcatInjectable

  • Creates an Injectable factory function without dependencies that appends a Service to an existing array of Services of the same type. Useful for dynamically expanding service collections without altering original service tokens or factories.

    Type Parameters

    • Token extends TokenType
    • Service

    Parameters

    • token: Token

      Token identifying an existing Service array to which the new Service will be appended.

    • fn: (() => Service)

      A no-argument function that returns the service to be appended.

    Returns InjectableFunction<{
        [T in keyof Token]: Service[]
    }, [], Token, Service[]>

    const container = Container
    .providesValue("values", [1]) // Initially provide an array with one value
    .provides(ConcatInjectable("values", () => 2)); // Append another value to the array

    const result = container.get("values"); // Results in [1, 2]

    In this context, ConcatInjectable("values", () => 2) acts as a simplified form of Injectable("values", ["values"], (values: number[]) => [...values, 2]), directly appending a new value to the "values" service array without the need for explicit array manipulation.

  • Creates an Injectable factory function with dependencies that appends a Service to an existing array of Services of the same type. This variant supports services that require other services to be instantiated, allowing for more complex setups.

    Type Parameters

    • Token extends TokenType
    • const Tokens extends readonly TokenType[]
    • Params extends readonly any[]
    • Service

    Parameters

    • token: Token

      Token identifying an existing Service array to append the new Service to.

    • dependencies: Tokens

      Read-only list of Tokens for dependencies required by the factory function.

    • fn: ((...args: Tokens["length"] extends Params["length"]
          ? Params
          : void[]) => Service)

      Factory function returning the Service to append. The types and number of its parameters must exactly match the dependencies.

    Returns InjectableFunction<ServicesFromTokenizedParams<Tokens, Params>, Tokens, Token, Service[]>

    const container = Container
    .providesValue("two", 2)
    .providesValue("values", [1]) // Initially provide an array with one value
    .provides(ConcatInjectable("values", ["two"] as const, (two: number) => two)); // Append another value to the array

    const result = container.get("values"); // [1, 2]