FuncType - v1.21.0
    Preparing search index...

    Interface Context<R>

    Context holds service implementations for dependency injection. It's an immutable container that maps Tags to their implementations.

    const ctx = Context.empty()
    .add(Logger, consoleLogger)
    .add(Database, pgDatabase)

    // Access a service
    const logger = ctx.get(Logger) // Option<Logger>
    const loggerUnsafe = ctx.unsafeGet(Logger) // Logger (throws if missing)
    interface Context<R extends Type> {
        size: number;
        add<S extends unknown>(tag: Tag<S>, service: S): Context<R & S>;
        get<S extends unknown>(tag: Tag<S>): Option<S>;
        has<S extends unknown>(tag: Tag<S>): boolean;
        merge<R2 extends unknown>(other: Context<R2>): Context<R & R2>;
        toString(): string;
        unsafeGet<S extends unknown>(tag: Tag<S>): S;
    }

    Type Parameters

    • R extends Type

      The services contained in this context (intersection type)

    Index
    size: number

    Returns the number of services in this context.

    • Adds a service to the context, returning a new context.

      Type Parameters

      • S extends unknown

      Parameters

      • tag: Tag<S>

        The tag for the service

      • service: S

        The service implementation

      Returns Context<R & S>

      A new context with the service added

    • Gets a service from the context.

      Type Parameters

      • S extends unknown

      Parameters

      • tag: Tag<S>

        The tag identifying the service

      Returns Option<S>

      Some(service) if found, None otherwise

    • Checks if a service exists in the context.

      Type Parameters

      • S extends unknown

      Parameters

      • tag: Tag<S>

        The tag to check

      Returns boolean

      true if the service exists

    • Merges another context into this one.

      Type Parameters

      • R2 extends unknown

      Parameters

      Returns Context<R & R2>

      A new context with all services from both

    • String representation

      Returns string

    • Gets a service from the context, throwing if not found.

      Type Parameters

      • S extends unknown

      Parameters

      • tag: Tag<S>

        The tag identifying the service

      Returns S

      The service

      Error if service not found