FuncType - v0.45.0
    Preparing search index...

    Interface ContextType<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 ContextType<R extends Type> {
        size: number;
        add<S extends unknown>(tag: TagType<S>, service: S): ContextType<R & S>;
        get<S extends unknown>(tag: TagType<S>): Option<S>;
        has<S extends unknown>(tag: TagType<S>): boolean;
        merge<R2 extends unknown>(other: ContextType<R2>): ContextType<R & R2>;
        toString(): string;
        unsafeGet<S extends unknown>(tag: TagType<S>): S;
    }

    Type Parameters

    • R extends Type

      The services contained in this context (intersection type)

    Index

    Properties

    Methods

    Properties

    size: number

    Returns the number of services in this context.

    Methods

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

      Type Parameters

      • S extends unknown

      Parameters

      • tag: TagType<S>

        The tag for the service

      • service: S

        The service implementation

      Returns ContextType<R & S>

      A new context with the service added

    • Gets a service from the context.

      Type Parameters

      • S extends unknown

      Parameters

      • tag: TagType<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

      Returns boolean

      true if the service exists

    • String representation

      Returns string

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

      Type Parameters

      • S extends unknown

      Parameters

      • tag: TagType<S>

        The tag identifying the service

      Returns S

      The service

      Error if service not found