Skip to content

Layer

A Layer<ROut, E, RIn> describes how to build one or more services in your application. Services can be injected into effects via Effect.provideService. Effects can require services via Effect.service.

Layer can be thought of as recipes for producing bundles of services, given their dependencies (other services).

Construction of services can be effectful and utilize resources that must be acquired and safely released when the services are done being utilized.

By default layers are shared, meaning that if the same layer is used twice the layer will only be allocated a single time.

Because of their excellent composition properties, layers are the idiomatic way in Effect-TS to create services that depend on other services.

96 exports Added in v2.0.0 Source

Clock

setClock

Added in v2.0.0 Source

Signature

declare const setClock: <A extends Clock.Clock>(clock: A) => Layer<never>;

Config

Sets the current ConfigProvider.

Signature

declare const setConfigProvider: (configProvider: ConfigProvider) => Layer<never>;

Constructors

context

Added in v2.0.0 Source

Constructs a Layer that passes along the specified context as an output.

Signature

declare const context: <R>() => Layer<R, never, R>;

die

Added in v2.0.0 Source

Constructs a layer that dies with the specified defect.

Signature

declare const die: (defect: unknown) => Layer<unknown>;

dieSync

Added in v2.0.0 Source

Constructs a layer that dies with the specified defect.

Signature

declare const dieSync: (evaluate: LazyArg<unknown>) => Layer<unknown>;

effect

Added in v2.0.0 Source

Constructs a layer from the specified effect.

Signature

declare const effect: {
  <I, S>(tag: Tag<I, S>): <E, R>(effect: Effect<NoInfer<S>, E, R>) => Layer<I, E, R>;
  <I, S, E, R>(tag: Tag<I, S>, effect: Effect<NoInfer<S>, E, R>): Layer<I, E, R>;
};

Constructs a layer from the specified effect, which must return one or more services.

Signature

declare const effectContext: <A, E, R>(
  effect: Effect.Effect<Context.Context<A>, E, R>,
) => Layer<A, E, R>;

Constructs a layer from the specified effect, discarding its output.

Signature

declare const effectDiscard: <X, E, R>(effect: Effect.Effect<X, E, R>) => Layer<never, E, R>;

empty

Added in v2.0.0 Source

A Layer that constructs an empty Context.

Signature

declare const empty: Layer<never>;

fail

Added in v2.0.0 Source

Constructs a layer that fails with the specified error.

Signature

declare const fail: <E>(error: E) => Layer<unknown, E>;

failCause

Added in v2.0.0 Source

Constructs a layer that fails with the specified cause.

Signature

declare const failCause: <E>(cause: Cause.Cause<E>) => Layer<unknown, E>;

Constructs a layer that fails with the specified cause.

Signature

declare const failCauseSync: <E>(evaluate: LazyArg<Cause.Cause<E>>) => Layer<unknown, E>;

failSync

Added in v2.0.0 Source

Constructs a layer that fails with the specified error.

Signature

declare const failSync: <E>(evaluate: LazyArg<E>) => Layer<unknown, E>;

scope

Added in v2.0.0 Source

A layer that constructs a scope and closes it when the workflow the layer is provided to completes execution, whether by success, failure, or interruption. This can be used to close a scope when providing a layer to a workflow.

Signature

declare const scope: Layer<Scope.Scope>;

scoped

Added in v2.0.0 Source

Constructs a layer from the specified scoped effect.

Signature

declare const scoped: {
  <I, S>(
    tag: Tag<I, S>,
  ): <E, R>(effect: Effect<NoInfer<S>, E, R>) => Layer<I, E, Exclude<R, Scope>>;
  <I, S, E, R>(tag: Tag<I, S>, effect: Effect<NoInfer<S>, E, R>): Layer<I, E, Exclude<R, Scope>>;
};

Constructs a layer from the specified scoped effect, which must return one or more services.

Signature

declare const scopedContext: <A, E, R>(
  effect: Effect.Effect<Context.Context<A>, E, R>,
) => Layer<A, E, Exclude<R, Scope.Scope>>;

Constructs a layer from the specified scoped effect, discarding its output.

Signature

declare const scopedDiscard: <X, E, R>(
  effect: Effect.Effect<X, E, R>,
) => Layer<never, E, Exclude<R, Scope.Scope>>;

service

Added in v2.0.0 Source

Constructs a layer that accesses and returns the specified service from the context.

Signature

declare const service: <I, S>(tag: Context.Tag<I, S>) => Layer<I, never, I>;

succeed

Added in v2.0.0 Source

Constructs a layer from the specified value.

Signature

declare const succeed: {
  <I, S>(tag: Tag<I, S>): (resource: NoInfer<S>) => Layer<I>;
  <I, S>(tag: Tag<I, S>, resource: NoInfer<S>): Layer<I>;
};

Constructs a layer from the specified value, which must return one or more services.

Signature

declare const succeedContext: <A>(context: Context.Context<A>) => Layer<A>;

suspend

Added in v2.0.0 Source

Lazily constructs a layer. This is useful to avoid infinite recursion when creating layers that refer to themselves.

Signature

declare const suspend: <RIn, E, ROut>(
  evaluate: LazyArg<Layer<ROut, E, RIn>>,
) => Layer<ROut, E, RIn>;

sync

Added in v2.0.0 Source

Lazily constructs a layer from the specified value.

Signature

declare const sync: {
  <I, S>(tag: Tag<I, S>): (evaluate: LazyArg<NoInfer<S>>) => Layer<I>;
  <I, S>(tag: Tag<I, S>, evaluate: LazyArg<NoInfer<S>>): Layer<I>;
};

syncContext

Added in v2.0.0 Source

Lazily constructs a layer from the specified value, which must return one or more services.

Signature

declare const syncContext: <A>(evaluate: LazyArg<Context.Context<A>>) => Layer<A>;

Conversions

launch

Added in v2.0.0 Source

Builds this layer and uses it until it is interrupted. This is useful when your entire application is a layer, such as an HTTP server.

Signature

declare const launch: <RIn, E, ROut>(self: Layer<ROut, E, RIn>) => Effect.Effect<never, E, RIn>;

toRuntime

Added in v2.0.0 Source

Converts a layer that requires no services into a scoped runtime, which can be used to execute effects.

Signature

declare const toRuntime: <RIn, E, ROut>(
  self: Layer<ROut, E, RIn>,
) => Effect.Effect<Runtime.Runtime<ROut>, E, Scope.Scope | RIn>;

Converts a layer that requires no services into a scoped runtime, which can be used to execute effects.

Signature

declare const toRuntimeWithMemoMap: {
  (
    memoMap: MemoMap,
  ): <RIn, E, ROut>(self: Layer<ROut, E, RIn>) => Effect<Runtime<ROut>, E, Scope | RIn>;
  <RIn, E, ROut>(
    self: Layer<ROut, E, RIn>,
    memoMap: MemoMap,
  ): Effect<Runtime<ROut>, E, Scope | RIn>;
};

Destructors

build

Added in v2.0.0 Source

Builds a layer into a scoped value.

Signature

declare const build: <RIn, E, ROut>(
  self: Layer<ROut, E, RIn>,
) => Effect.Effect<Context.Context<ROut>, E, Scope.Scope | RIn>;

Builds a layer into an Effect value. Any resources associated with this layer will be released when the specified scope is closed unless their scope has been extended. This allows building layers where the lifetime of some of the services output by the layer exceed the lifetime of the effect the layer is provided to.

Signature

declare const buildWithScope: {
  (scope: Scope): <RIn, E, ROut>(self: Layer<ROut, E, RIn>) => Effect<Context<ROut>, E, RIn>;
  <RIn, E, ROut>(self: Layer<ROut, E, RIn>, scope: Scope): Effect<Context<ROut>, E, RIn>;
};

Error Handling

catchAll

Added in v2.0.0 Source

Recovers from all errors.

Signature

declare const catchAll: {
  <E, RIn2, E2, ROut2>(
    onError: (error: E) => Layer<ROut2, E2, RIn2>,
  ): <RIn, ROut>(self: Layer<ROut, E, RIn>) => Layer<ROut & ROut2, E2, RIn2 | RIn>;
  <RIn, E, ROut, RIn2, E2, ROut2>(
    self: Layer<ROut, E, RIn>,
    onError: (error: E) => Layer<ROut2, E2, RIn2>,
  ): Layer<ROut & ROut2, E2, RIn | RIn2>;
};

Recovers from all errors.

Signature

declare const catchAllCause: {
  <E, RIn2, E2, ROut2>(
    onError: (cause: Cause<E>) => Layer<ROut2, E2, RIn2>,
  ): <RIn, ROut>(self: Layer<ROut, E, RIn>) => Layer<ROut & ROut2, E2, RIn2 | RIn>;
  <RIn, E, ROut, RIn2, E2, ROut22>(
    self: Layer<ROut, E, RIn>,
    onError: (cause: Cause<E>) => Layer<ROut22, E2, RIn2>,
  ): Layer<ROut & ROut22, E2, RIn | RIn2>;
};

orDie

Added in v2.0.0 Source

Translates effect failure into death of the fiber, making all failures unchecked and not a part of the type of the layer.

Signature

declare const orDie: <A, E, R>(self: Layer<A, E, R>) => Layer<A, never, R>;

orElse

Added in v2.0.0 Source

Executes this layer and returns its output, if it succeeds, but otherwise executes the specified layer.

Signature

declare const orElse: {
  <A2, E2, R2>(
    that: LazyArg<Layer<A2, E2, R2>>,
  ): <A, E, R>(self: Layer<A, E, R>) => Layer<A & A2, E2 | E, R2 | R>;
  <A, E, R, A2, E2, R2>(
    self: Layer<A, E, R>,
    that: LazyArg<Layer<A2, E2, R2>>,
  ): Layer<A & A2, E | E2, R | R2>;
};

Folding

match

Added in v2.0.0 Source

Feeds the error or output services of this layer into the input of either the specified failure or success layers, resulting in a new layer with the inputs of this layer, and the error or outputs of the specified layer.

Signature

declare const match: {
  <E, A2, E2, R2, A, A3, E3, R3>(options: {
    readonly onFailure: (error: E) => Layer<A2, E2, R2>;
    readonly onSuccess: (context: Context.Context<A>) => Layer<A3, E3, R3>;
  }): <R>(self: Layer<A, E, R>) => Layer<A2 & A3, E2 | E3, R2 | R3 | R>;
  <A, E, R, A2, E2, R2, A3, E3, R3>(
    self: Layer<A, E, R>,
    options: {
      readonly onFailure: (error: E) => Layer<A2, E2, R2>;
      readonly onSuccess: (context: Context.Context<A>) => Layer<A3, E3, R3>;
    },
  ): Layer<A2 & A3, E2 | E3, R | R2 | R3>;
};

matchCause

Added in v2.0.0 Source

Feeds the error or output services of this layer into the input of either the specified failure or success layers, resulting in a new layer with the inputs of this layer, and the error or outputs of the specified layer.

Signature

declare const matchCause: {
  <E, A2, E2, R2, A, A3, E3, R3>(options: {
    readonly onFailure: (cause: Cause.Cause<E>) => Layer<A2, E2, R2>;
    readonly onSuccess: (context: Context.Context<A>) => Layer<A3, E3, R3>;
  }): <R>(self: Layer<A, E, R>) => Layer<A2 & A3, E2 | E3, R2 | R3 | R>;
  <A, E, R, A2, E2, R2, A3, E3, R3>(
    self: Layer<A, E, R>,
    options: {
      readonly onFailure: (cause: Cause.Cause<E>) => Layer<A2, E2, R2>;
      readonly onSuccess: (context: Context.Context<A>) => Layer<A3, E3, R3>;
    },
  ): Layer<A2 & A3, E2 | E3, R | R2 | R3>;
};

Getters

isFresh

Added in v2.0.0 Source

Returns true if the specified Layer is a fresh version that will not be shared, false otherwise.

Signature

declare const isFresh: <RIn, E, ROut>(self: Layer<ROut, E, RIn>) => boolean;

isLayer

Added in v2.0.0 Source

Returns true if the specified value is a Layer, false otherwise.

Signature

declare const isLayer: (u: unknown) => u is Layer<unknown, unknown, unknown>;

Logging

Signature

declare const setUnhandledErrorLogLevel: (level: Option.Option<LogLevel>) => Layer<never>;

Signature

declare const setVersionMismatchErrorLogLevel: (level: Option.Option<LogLevel>) => Layer<never>;

Mapping

discard

Added in v2.0.0 Source

Replaces the layer's output with never and includes the layer only for its side-effects.

Signature

declare const discard: <RIn, E, ROut>(self: Layer<ROut, E, RIn>) => Layer<never, E, RIn>;

map

Added in v2.0.0 Source

Returns a new layer whose output is mapped by the specified function.

Signature

declare const map: {
  <A, B>(f: (context: Context<A>) => Context<B>): <E, R>(self: Layer<A, E, R>) => Layer<B, E, R>;
  <A, E, R, B>(self: Layer<A, E, R>, f: (context: Context<A>) => Context<B>): Layer<B, E, R>;
};

mapError

Added in v2.0.0 Source

Returns a layer with its error channel mapped using the specified function.

Signature

declare const mapError: {
  <E, E2>(f: (error: E) => E2): <A, R>(self: Layer<A, E, R>) => Layer<A, E2, R>;
  <A, E, R, E2>(self: Layer<A, E, R>, f: (error: E) => E2): Layer<A, E2, R>;
};

Memo Map

Builds a layer into an Effect value, using the specified MemoMap to memoize the layer construction.

Signature

declare const buildWithMemoMap: {
  (
    memoMap: MemoMap,
    scope: Scope,
  ): <RIn, E, ROut>(self: Layer<ROut, E, RIn>) => Effect<Context<ROut>, E, RIn>;
  <RIn, E, ROut>(
    self: Layer<ROut, E, RIn>,
    memoMap: MemoMap,
    scope: Scope,
  ): Effect<Context<ROut>, E, RIn>;
};

makeMemoMap

Added in v2.0.0 Source

Constructs a MemoMap that can be used to build additional layers.

Signature

declare const makeMemoMap: Effect.Effect<MemoMap>;

Models

CurrentMemoMap

Added in v3.13.0 Source

Signature

declare const CurrentMemoMap: Reference<CurrentMemoMap, MemoMap>;

CurrentMemoMap interface

Added in v3.13.0 Source

Signature

interface CurrentMemoMap {
  readonly _: typeof _;
}

Layer interface

Added in v2.0.0 Source

Signature

interface Layer<in ROut, out E = never, out RIn = never> extends Variance<ROut, E, RIn>, Pipeable {}

MemoMap interface

Added in v2.0.0 Source

Signature

interface MemoMap {
  readonly [MemoMapTypeId]: typeof MemoMapTypeId;
}

Other

Signature

declare const function: <I1, S1, I2, S2>(tagA: Context.Tag<I1, S1>, tagB: Context.Tag<I2, S2>, f: (a: Types.NoInfer<S1>) => Types.NoInfer<S2>) => Layer<I2, never, I1>

Layer

Added in v2.0.0 Source

Random

setRandom

Added in v3.15.0 Source

Signature

declare function setRandom<A extends Random>(random: A): Layer<never>;

Requests & Batching

Signature

declare const setRequestBatching: (requestBatching: boolean) => Layer<never>;

Signature

declare const setRequestCache: {
  <E, R>(cache: Effect<Cache, E, R>): Layer<never, E, Exclude<R, Scope>>;
  (cache: Cache): Layer<never>;
};

Signature

declare const setRequestCaching: (requestCaching: boolean) => Layer<never>;

Retrying

retry

Added in v2.0.0 Source

Retries constructing this layer according to the specified schedule.

Signature

declare const retry: {
  <X, E, RIn2>(
    schedule: Schedule<X, NoInfer<E>, RIn2>,
  ): <ROut, RIn>(self: Layer<ROut, E, RIn>) => Layer<ROut, E, RIn2 | RIn>;
  <ROut, E, RIn, X, RIn2>(
    self: Layer<ROut, E, RIn>,
    schedule: Schedule<X, E, RIn2>,
  ): Layer<ROut, E, RIn | RIn2>;
};

Scheduler

setScheduler

Added in v2.0.0 Source

Signature

declare const setScheduler: (scheduler: Scheduler.Scheduler) => Layer<never>;

Sequencing

flatMap

Added in v2.0.0 Source

Constructs a layer dynamically based on the output of this layer.

Signature

declare const flatMap: {
  <A, A2, E2, R2>(
    f: (context: Context<A>) => Layer<A2, E2, R2>,
  ): <E, R>(self: Layer<A, E, R>) => Layer<A2, E2 | E, R2 | R>;
  <A, E, R, A2, E2, R2>(
    self: Layer<A, E, R>,
    f: (context: Context<A>) => Layer<A2, E2, R2>,
  ): Layer<A2, E | E2, R | R2>;
};

flatten

Added in v2.0.0 Source

Flattens layers nested in the context of an effect.

Signature

declare const flatten: {
  <I, A, E2, R2>(
    tag: Tag<I, Layer<A, E2, R2>>,
  ): <E, R>(self: Layer<I, E, R>) => Layer<A, E2 | E, R2 | R>;
  <I, E, R, A, E2, R2>(
    self: Layer<I, E, R>,
    tag: Tag<I, Layer<A, E2, R2>>,
  ): Layer<A, E | E2, R | R2>;
};

tap

Added in v2.0.0 Source

Performs the specified effect if this layer succeeds.

Signature

declare const tap: {
  <ROut, XR, RIn2, E2, X>(
    f: (context: Context<XR>) => Effect<X, E2, RIn2>,
  ): <RIn, E>(self: Layer<ROut, E, RIn>) => Layer<ROut, E2 | E, RIn2 | RIn>;
  <RIn, E, ROut, XR, RIn2, E2, X>(
    self: Layer<ROut, E, RIn>,
    f: (context: Context<XR>) => Effect<X, E2, RIn2>,
  ): Layer<ROut, E | E2, RIn | RIn2>;
};

tapError

Added in v2.0.0 Source

Performs the specified effect if this layer fails.

Signature

declare const tapError: {
  <E, XE, RIn2, E2, X>(
    f: (e: XE) => Effect<X, E2, RIn2>,
  ): <RIn, ROut>(self: Layer<ROut, E, RIn>) => Layer<ROut, E | E2, RIn2 | RIn>;
  <RIn, E, XE, ROut, RIn2, E2, X>(
    self: Layer<ROut, E, RIn>,
    f: (e: XE) => Effect<X, E2, RIn2>,
  ): Layer<ROut, E | E2, RIn | RIn2>;
};

Performs the specified effect if this layer fails.

Signature

declare const tapErrorCause: {
  <E, XE, RIn2, E2, X>(
    f: (cause: Cause<XE>) => Effect<X, E2, RIn2>,
  ): <RIn, ROut>(self: Layer<ROut, E, RIn>) => Layer<ROut, E | E2, RIn2 | RIn>;
  <RIn, E, XE, ROut, RIn2, E2, X>(
    self: Layer<ROut, E, RIn>,
    f: (cause: Cause<XE>) => Effect<X, E2, RIn2>,
  ): Layer<ROut, E | E2, RIn | RIn2>;
};

Symbols

LayerTypeId

Added in v2.0.0 Source

Signature

declare const LayerTypeId: unique symbol;

LayerTypeId type

Added in v2.0.0 Source

Signature

type LayerTypeId = typeof LayerTypeId;

Signature

declare const MemoMapTypeId: unique symbol;

MemoMapTypeId type

Added in v2.0.0 Source

Signature

type MemoMapTypeId = typeof MemoMapTypeId;

Testing

mock

Added in v3.17.0 Source

Creates a mock layer for testing purposes. You can provide a partial implementation of the service, and any methods not provided will throw an UnimplementedError defect when called.

Signature

declare const mock: {
  <I, S extends object>(
    tag: Tag<I, S>,
  ): (
    service: Simplify<
      { [K in string | number | symbol]: S[K] } & { [K in string | number | symbol]: S[K] }
    >,
  ) => Layer<I>;
  <I, S extends object>(
    tag: Tag<I, S>,
    service: Simplify<
      { [K in string | number | symbol]: S[K] } & { [K in string | number | symbol]: S[K] }
    >,
  ): Layer<I>;
};

Example

import { Context, Effect, Layer } from "effect"

class MyService extends Context.Tag("MyService")<
  MyService,
  {
    one: Effect.Effect<number>
    two(): Effect.Effect<number>
  }
>() {}

const MyServiceTest = Layer.mock(MyService, {
  two: () => Effect.succeed(2),
})

PartialEffectful type

Added in v3.17.0 Source

Signature

type PartialEffectful<A extends object> = Types.Simplify<
  { [K in keyof A]: A[K] } & { [K in keyof A]: A[K] }
>;

Tracing

annotateLogs

Added in v3.3.0 Source

Signature

declare const annotateLogs: {
  (key: string, value: unknown): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, R>;
  (values: Record<string, unknown>): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, R>;
  <A, E, R>(self: Layer<A, E, R>, key: string, value: unknown): Layer<A, E, R>;
  <A, E, R>(self: Layer<A, E, R>, values: Record<string, unknown>): Layer<A, E, R>;
};

Signature

declare const annotateSpans: {
  (key: string, value: unknown): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, R>;
  (values: Record<string, unknown>): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, R>;
  <A, E, R>(self: Layer<A, E, R>, key: string, value: unknown): Layer<A, E, R>;
  <A, E, R>(self: Layer<A, E, R>, values: Record<string, unknown>): Layer<A, E, R>;
};

parentSpan

Added in v2.0.0 Source

Adds the provided span to the span stack.

Signature

declare const parentSpan: (span: Tracer.AnySpan) => Layer<Tracer.ParentSpan>;

setTracer

Added in v2.0.0 Source

Create a Layer that sets the current Tracer

Signature

declare const setTracer: (tracer: Tracer.Tracer) => Layer<never>;

Signature

declare const setTracerEnabled: (enabled: boolean) => Layer<never>;

Signature

declare const setTracerTiming: (enabled: boolean) => Layer<never>;

span

Added in v2.0.0 Source

Create and add a span to the current span stack.

The span is ended when the Layer is released.

Signature

declare const span: (
  name: string,
  options?: Tracer.SpanOptions & {
    readonly onEnd?: (span: Tracer.Span, exit: Exit.Exit<unknown, unknown>) => Effect.Effect<void>;
  },
) => Layer<Tracer.ParentSpan>;

Signature

declare const withParentSpan: {
  (span: AnySpan): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, Exclude<R, ParentSpan>>;
  <A, E, R>(self: Layer<A, E, R>, span: AnySpan): Layer<A, E, Exclude<R, ParentSpan>>;
};

withSpan

Added in v2.0.0 Source

Signature

declare const withSpan: {
  (
    name: string,
    options?: SpanOptions & {
      readonly onEnd?: (
        span: Tracer.Span,
        exit: Exit.Exit<unknown, unknown>,
      ) => Effect.Effect<void>;
    },
  ): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, Exclude<R, ParentSpan>>;
  <A, E, R>(
    self: Layer<A, E, R>,
    name: string,
    options?: SpanOptions & {
      readonly onEnd?: (
        span: Tracer.Span,
        exit: Exit.Exit<unknown, unknown>,
      ) => Effect.Effect<void>;
    },
  ): Layer<A, E, Exclude<R, ParentSpan>>;
};

Type Constraints

A no-op type constraint that enforces the error channel of a Layer conforms to the specified error type E.

Signature

declare function ensureErrorType<E>(): <ROut, E2, RIn>(
  layer: Layer<ROut, E2, RIn>,
) => Layer<ROut, E2, RIn>;

A no-op type constraint that enforces the requirements channel of a Layer conforms to the specified requirements type RIn.

Signature

declare function ensureRequirementsType<RIn>(): <ROut, E, RIn2>(
  layer: Layer<ROut, E, RIn2>,
) => Layer<ROut, E, RIn2>;

A no-op type constraint that enforces the success channel of a Layer conforms to the specified success type ROut.

Signature

declare function ensureSuccessType<ROut>(): <ROut2, E, RIn>(
  layer: Layer<ROut2, E, RIn>,
) => Layer<ROut2, E, RIn>;

Utils

extendScope

Added in v2.0.0 Source

Extends the scope of this layer, returning a new layer that when provided to an effect will not immediately release its associated resources when that effect completes execution but instead when the scope the resulting effect depends on is closed.

Signature

declare const extendScope: <RIn, E, ROut>(
  self: Layer<ROut, E, RIn>,
) => Layer<ROut, E, Scope.Scope | RIn>;

Signature

declare const fiberRefLocallyScopedWith: <A>(self: FiberRef<A>, value: (_: A) => A) => Layer<never>;

fresh

Added in v2.0.0 Source

Creates a fresh version of this layer that will not be shared.

Signature

declare const fresh: <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, R>;

locally

Added in v2.0.0 Source

Signature

declare const locally: {
  <X>(ref: FiberRef<X>, value: X): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, R>;
  <A, E, R, X>(self: Layer<A, E, R>, ref: FiberRef<X>, value: X): Layer<A, E, R>;
};

Signature

declare const locallyEffect: {
  <RIn, E, ROut, RIn2, E2, ROut2>(
    f: (_: Effect<RIn, E, Context<ROut>>) => Effect<RIn2, E2, Context<ROut2>>,
  ): (self: Layer<ROut, E, RIn>) => Layer<ROut2, E2, RIn2>;
  <RIn, E, ROut, RIn2, E2, ROut2>(
    self: Layer<ROut, E, RIn>,
    f: (_: Effect<RIn, E, Context<ROut>>) => Effect<RIn2, E2, Context<ROut2>>,
  ): Layer<ROut2, E2, RIn2>;
};

Signature

declare const locallyScoped: <A>(self: FiberRef<A>, value: A) => Layer<never>;

locallyWith

Added in v2.0.0 Source

Signature

declare const locallyWith: {
  <X>(ref: FiberRef<X>, value: (_: X) => X): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, R>;
  <A, E, R, X>(self: Layer<A, E, R>, ref: FiberRef<X>, value: (_: X) => X): Layer<A, E, R>;
};

memoize

Added in v2.0.0 Source

Returns a scoped effect that, if evaluated, will return the lazily computed result of this layer.

Signature

declare const memoize: <RIn, E, ROut>(
  self: Layer<ROut, E, RIn>,
) => Effect.Effect<Layer<ROut, E, RIn>, never, Scope.Scope>;

passthrough

Added in v2.0.0 Source

Returns a new layer that produces the outputs of this layer but also passes through the inputs.

Signature

declare const passthrough: <RIn, E, ROut>(self: Layer<ROut, E, RIn>) => Layer<RIn | ROut, E, RIn>;

project

Added in v2.0.0 Source

Projects out part of one of the services output by this layer using the specified function.

Signature

declare const project: {
  <I1, S1, I2, S2>(
    tagA: Tag<I1, S1>,
    tagB: Tag<I2, S2>,
    f: (a: NoInfer<S1>) => NoInfer<S2>,
  ): <RIn, E>(self: Layer<I1, E, RIn>) => Layer<I2, E, RIn>;
  <RIn, E, I1, S1, I2, S2>(
    self: Layer<I1, E, RIn>,
    tagA: Tag<I1, S1>,
    tagB: Tag<I2, S2>,
    f: (a: NoInfer<S1>) => NoInfer<S2>,
  ): Layer<I2, E, RIn>;
};

provide

Added in v2.0.0 Source

Feeds the output services of this builder into the input of the specified builder, resulting in a new builder with the inputs of this builder as well as any leftover inputs, and the outputs of the specified builder.

Signature

declare const provide: {
  <RIn, E, ROut>(
    that: Layer<ROut, E, RIn>,
  ): <RIn2, E2, ROut2>(
    self: Layer<ROut2, E2, RIn2>,
  ) => Layer<ROut2, E | E2, RIn | Exclude<RIn2, ROut>>;
  <Layers extends readonly [Any, Any]>(
    that: Layers,
  ): <A, E, R>(
    self: Layer<A, E, R>,
  ) => Layer<
    A,
    E | { [k in string | number | symbol]: Error<Layers[k]> }[number],
    | { [k in string | number | symbol]: Context<Layers[k]> }[number]
    | Exclude<R, { [k in string | number | symbol]: Success<Layers[k]> }[number]>
  >;
  <RIn2, E2, ROut2, RIn, E, ROut>(
    self: Layer<ROut2, E2, RIn2>,
    that: Layer<ROut, E, RIn>,
  ): Layer<ROut2, E2 | E, RIn | Exclude<RIn2, ROut>>;
  <A, E, R, Layers extends readonly [Any, Any]>(
    self: Layer<A, E, R>,
    that: Layers,
  ): Layer<
    A,
    E | { [k in string | number | symbol]: Error<Layers[k]> }[number],
    | { [k in string | number | symbol]: Context<Layers[k]> }[number]
    | Exclude<R, { [k in string | number | symbol]: Success<Layers[k]> }[number]>
  >;
};

provideMerge

Added in v2.0.0 Source

Feeds the output services of this layer into the input of the specified layer, resulting in a new layer with the inputs of this layer, and the outputs of both layers.

Signature

declare const provideMerge: {
  <RIn, E, ROut>(
    self: Layer<ROut, E, RIn>,
  ): <RIn2, E2, ROut2>(
    that: Layer<ROut2, E2, RIn2>,
  ) => Layer<ROut | ROut2, E | E2, RIn | Exclude<RIn2, ROut>>;
  <RIn2, E2, ROut2, RIn, E, ROut>(
    that: Layer<ROut2, E2, RIn2>,
    self: Layer<ROut, E, RIn>,
  ): Layer<ROut2 | ROut, E2 | E, RIn | Exclude<RIn2, ROut>>;
};

unwrapEffect

Added in v2.0.0 Source

Signature

declare const unwrapEffect: <A, E1, R1, E, R>(
  self: Effect.Effect<Layer<A, E1, R1>, E, R>,
) => Layer<A, E | E1, R | R1>;

unwrapScoped

Added in v2.0.0 Source

Signature

declare const unwrapScoped: <A, E1, R1, E, R>(
  self: Effect.Effect<Layer<A, E1, R1>, E, R>,
) => Layer<A, E | E1, R1 | Exclude<R, Scope.Scope>>;

updateService

Added in v3.13.0 Source

Updates a service in the context with a new implementation.

Details

This function modifies the existing implementation of a service in the context. It retrieves the current service, applies the provided transformation function f, and replaces the old service with the transformed one.

When to Use

This is useful for adapting or extending a service's behavior during the creation of a layer.

Signature

declare const updateService: <I, A>(tag: Tag<I, A>, f: (a: A) => A) => <A1, E1, R1>(layer: Layer<A1, E1, R1>) => Layer<A1, E1, I | R1> & <A1, E1, R1, I, A>(layer: Layer<A1, E1, R1>, tag: Tag<I, A>, f: (a: A) => A) => Layer<A1, E1, R1 | I>

Zipping

merge

Added in v2.0.0 Source

Merges this layer with the specified layer concurrently, producing a new layer with combined input and output types.

Signature

declare const merge: {
  <RIn2, E2, ROut2>(
    that: Layer<ROut2, E2, RIn2>,
  ): <RIn, E1, ROut>(self: Layer<ROut, E1, RIn>) => Layer<ROut2 | ROut, E2 | E1, RIn2 | RIn>;
  <RIn, E1, ROut, RIn2, E2, ROut2>(
    self: Layer<ROut, E1, RIn>,
    that: Layer<ROut2, E2, RIn2>,
  ): Layer<ROut | ROut2, E1 | E2, RIn | RIn2>;
};

mergeAll

Added in v2.0.0 Source

Combines all the provided layers concurrently, creating a new layer with merged input, error, and output types.

Signature

declare const mergeAll: <
  Layers extends readonly [Layer<never, any, any>, ...Array<Layer<never, any, any>>],
>(
  ...layers: Layers
) => Layer<
  { [k in keyof Layers]: Layer.Success<Layers[k]> }[number],
  { [k in keyof Layers]: Layer.Error<Layers[k]> }[number],
  { [k in keyof Layers]: Layer.Context<Layers[k]> }[number]
>;

zipWith

Added in v2.0.0 Source

Combines this layer with the specified layer concurrently, creating a new layer with merged input types and combined output types using the provided function.

Signature

declare const zipWith: {
  <B, E2, R2, A, C>(
    that: Layer<B, E2, R2>,
    f: (a: Context<A>, b: Context<B>) => Context<C>,
  ): <E, R>(self: Layer<A, E, R>) => Layer<C, E2 | E, R2 | R>;
  <A, E, R, B, E2, R2, C>(
    self: Layer<A, E, R>,
    that: Layer<B, E2, R2>,
    f: (a: Context<A>, b: Context<B>) => Context<C>,
  ): Layer<C, E | E2, R | R2>;
};