Layer
Builds and wires services for Effect applications.
A Layer<ROut, E, RIn> describes how to acquire one or more services, which services are required to build them, and which errors can occur during acquisition. Layers can manage scoped resources, memoize shared services, combine with other layers, provide services to effects or streams, and attach error handling, tracing, or lifecycle hooks.
Constructors
Signature
declare const effect: {
<I, S>(service: Key<I, S>): <E, R>(effect: Effect<S, E, R>) => Layer<I, E, Exclude<R, Scope>>;
<I, S, E, R>(
service: Key<I, S>,
effect: Effect<NoInfer<S>, E, R>,
): Layer<I, E, Exclude<R, Scope>>;
};effectContext
Constructs a layer from an effect that produces all services in a Context.
When to use
Use when you need a Layer that effectfully constructs a Context with multiple services.
Details
This allows you to create a Layer from an effectful computation that returns multiple services. The Effect is executed in the scope of the layer.
See
effectfor effectfully providing a single service
Signature
declare function effectContext<A, E, R>(
effect: Effect<Context<A>, E, R>,
): Layer<A, E, Exclude<R, Scope>>;effectDiscard
Constructs a layer from an effect, discarding its value and providing no services.
When to use
Use when layer construction should run an Effect for its side effects while providing no services.
See
emptyfor a no-op layer that performs no construction work
Signature
declare function effectDiscard<X, E, R>(
effect: Effect<X, E, R>,
): Layer<never, E, Exclude<R, Scope>>;An empty layer that provides no services, cannot fail, has no requirements, and performs no construction or finalization work.
When to use
Use as the no-op branch when conditionally composing layers.
See
effectDiscardfor running an effect while providing no services
Signature
declare const empty: Layer<never>;forkMemoMap
Constructs a child MemoMap effectfully, allowing it to reuse layers already memoized in the parent while isolating any new layer allocations to the child map.
When to use
Use when a layer build should inherit already memoized layers from an existing MemoMap while keeping newly memoized layers out of the parent map.
See
makeMemoMapfor creating a root memo map in anEffectforkMemoMapUnsafefor the synchronous constructor variantbuildWithMemoMapfor building layers with an explicit memo map
Signature
declare function forkMemoMap(parent: MemoMap): Effect<MemoMap>;forkMemoMapUnsafe
Constructs a child MemoMap synchronously, allowing it to reuse layers already memoized in the parent while isolating any new layer allocations to the child map.
When to use
Use to synchronously fork a memo map for manual layer building when child builds should see parent memoized layers without writing newly built layers back to the parent.
See
forkMemoMapfor allocating the child memo map insideEffectmakeMemoMapUnsafefor creating a root memo map without a parent
Signature
declare function forkMemoMapUnsafe(parent: MemoMap): MemoMap;Constructs a Layer from a function that uses a MemoMap and Scope to build the layer.
Details
The function receives a MemoMap for memoization and a Scope for resource management. A child scope is created, and if the build fails, the child scope is closed.
Signature
declare function fromBuild<ROut, E, RIn>(
build: (memoMap: MemoMap, scope: Scope) => Effect<Context<ROut>, E, RIn>,
): Layer<ROut, E, RIn>;fromBuildMemo
Constructs a Layer from a function that uses a MemoMap and Scope to build the layer, with automatic memoization.
Details
This is similar to fromBuild but provides automatic memoization of the layer construction. The layer will be memoized based on the provided MemoMap.
Signature
declare function fromBuildMemo<ROut, E, RIn>(
build: (memoMap: MemoMap, scope: Scope) => Effect<Context<ROut>, E, RIn>,
): Layer<ROut, E, RIn>;makeMemoMap
Constructs a MemoMap effectfully so it can be used to build additional layers.
Signature
declare const makeMemoMap: Effect<MemoMap>;makeMemoMapUnsafe
Constructs a MemoMap synchronously so it can be used to build additional layers.
Signature
declare function makeMemoMapUnsafe(): MemoMap;Constructs a layer that provides a single service from an already available value.
When to use
Use when you need a Layer that provides a service from an already constructed implementation without effectful acquisition.
See
syncfor constructing layers from lazy values
Signature
declare const succeed: {
<I, S>(service: Key<I, S>): (resource: S) => Layer<I>;
<I, S>(service: Key<I, S>, resource: NoInfer<S>): Layer<I>;
};succeedContext
Constructs a layer that provides all services in an already available Context.
When to use
Use when you need a Layer built from an existing Context, including when you need to provide multiple services at once.
Details
This is a more general version of succeed that allows you to provide multiple services at once through a Context.
See
succeedfor providing a single service from a value
Signature
declare function succeedContext<A>(context: Context<A>): Layer<A>;Constructs a layer lazily using the specified factory.
Details
The factory is evaluated only when the suspended layer is first built, and the result is memoized with normal layer sharing semantics.
Signature
declare function suspend<A, E, R>(evaluate: LazyArg<Layer<A, E, R>>): Layer<A, E, R>;Constructs a layer lazily that provides a single service.
When to use
Use when you need a Layer that provides one service whose value is created synchronously, but creation should be deferred until the layer is built.
Details
This is a lazy version of succeed where the service value is computed synchronously only when the layer is built.
See
succeedfor constructing layers from static values
Signature
declare const sync: {
<I, S>(service: Key<I, S>): (evaluate: LazyArg<S>) => Layer<I>;
<I, S>(service: Key<I, S>, evaluate: LazyArg<NoInfer<S>>): Layer<I>;
};syncContext
Constructs a layer lazily that provides all services in a Context.
When to use
Use when you need a Layer that creates multiple services synchronously but defers that work until the layer is built.
Details
This is a lazy version of succeedContext where the Context is computed synchronously only when the layer is built.
See
syncfor lazily providing a single servicesucceedContextfor providing an already available context
Signature
declare function syncContext<A>(evaluate: LazyArg<Context<A>>): Layer<A>;Converting
Builds this layer and keeps it alive until the returned effect is interrupted.
When to use
Use when you model your entire application as a layer, such as an HTTP server.
Details
When the returned effect is interrupted, the layer scope is closed and all finalizers registered during layer acquisition are run.
Signature
declare function launch<RIn, E, ROut>(self: Layer<ROut, E, RIn>): Effect<never, E, RIn>;Unwraps a Layer from an Effect, flattening the nested structure.
When to use
Use when you have an Effect that produces a Layer and you want to use that layer directly.
Details
The resulting Layer will have the combined error and dependency types from both the outer Effect and the inner Layer.
Signature
declare function unwrap<A, E1, R1, E, R>(
self: Effect<Layer<A, E1, R1>, E, R>,
): Layer<A, E1 | E, R1 | Exclude<R, Scope>>;Destructors
Builds a layer into a scoped value.
Signature
declare function build<RIn, E, ROut>(
self: Layer<ROut, E, RIn>,
): Effect<Context<ROut>, E, Scope | RIn>;buildWithMemoMap
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>;
};buildWithScope
Builds a layer using an explicit scope.
When to use
Use to control the lifetime of layer resources with a scope supplied by the caller.
Details
Resources created by the layer are released when the supplied scope is closed, unless a resource extends its own scope.
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
catchCause
Recovers from any failure cause by switching to another layer.
When to use
Use when you need Layer recovery to inspect more than the typed error, such as defects or interruption information.
Details
The handler receives the full Cause of the failed layer, including typed errors, unexpected defects, and interruption information, and returns the fallback layer to build instead. Finalizers for resources acquired by the failed layer are still run before the fallback layer is acquired.
See
catchTagfor recovering from specific tagged errors
Signature
declare const catchCause: {
<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>;
};Recovers from specific tagged errors.
When to use
Use when only some tagged Layer construction errors should be recovered.
See
catchCausefor recovering with access to the full cause
Signature
declare const catchTag: {
<K extends string | readonly [Tags<E>, Tags<E>], E, RIn2, E2, ROut2>(
k: K,
f: (
e: ExtractTag<NoInfer<E>, K extends readonly [string, string] ? K[number] : K>,
) => Layer<ROut2, E2, RIn2>,
): <RIn, ROut>(
self: Layer<ROut, E, RIn>,
) => Layer<
ROut & ROut2,
| E2
| Exclude<
E,
{
readonly _tag: K;
}
>,
RIn2 | RIn
>;
<RIn, E, ROut, K extends string | readonly [Tags<E>, Tags<E>], RIn2, E2, ROut2>(
self: Layer<ROut, E, RIn>,
k: K,
f: (
e: ExtractTag<E, K extends readonly [string, string] ? K[number] : K>,
) => Layer<ROut2, E2, RIn2>,
): Layer<
ROut & ROut2,
| E2
| Exclude<
E,
{
readonly _tag: K;
}
>,
RIn | RIn2
>;
};Converts layer construction failures into defects, removing them from the layer's error type.
Details
Use this only when failures should be treated as unrecoverable defects rather than typed errors that callers can handle.
Signature
declare function orDie<A, E, R>(self: Layer<A, E, R>): Layer<A, never, R>;Guards
Layers
Creates a fresh version of this layer that will not be shared.
When to use
Use when you need two parts of an application to receive separate instances of a resource, such as two independent client sessions.
Gotchas
Do not use it just to work around confusing composition. By default, sharing the same layer value is usually the desired behavior.
Signature
declare function fresh<A, E, R>(self: Layer<A, E, R>): Layer<A, E, R>;Models
A Layer describes how to build one or more services for dependency injection.
When to use
Use to model construction of application services for dependency injection, especially when services have dependencies, can fail during construction, or need scoped setup and release.
Details
A Layer<ROut, E, RIn> represents ROut as the services this layer provides, E as the possible errors during layer construction, and RIn as the services this layer requires as dependencies.
Signature
interface Layer<in ROut, out E = never, out RIn = never> extends Variance<ROut, E, RIn>, Pipeable {
[ignoreSymbol]?: LayerUnifyIgnore;
[typeSymbol]?: unknown;
[unifySymbol]?: LayerUnify<Layer<ROut, E, RIn>>;
}LayerUnify interface
Type-level hook that allows Layer values to participate in Unify inference.
Details
This is used by Effect's pipe and unification machinery to preserve the provided services, error, and requirements of a Layer.
Signature
interface LayerUnify<
A extends {
[typeSymbol]?: any;
},
> {
Layer?: () => A[typeof typeSymbol] extends Layer<any, any, any> | _
? Layer<
Success<Extract<any[any], Any>>,
Error<Extract<any[any], Any>>,
Services<Extract<any[any], Any>>
>
: never;
}LayerUnifyIgnore interface
Type-level marker used by Unify for Layer types that should be ignored during unification.
Signature
interface LayerUnifyIgnore {}A MemoMap is used to memoize layer construction and ensure sharing of layers.
Details
The MemoMap prevents duplicate construction of the same layer instance, enabling efficient resource sharing across layer dependencies.
Signature
interface MemoMap {
readonly "~effect/Layer/MemoMap": "~effect/Layer/MemoMap";
readonly get: <RIn, E, ROut>(
layer: Layer<ROut, E, RIn>,
scope: Scope,
) => Effect<Context<ROut>, E, RIn> | undefined;
readonly getOrElseMemoize: <RIn, E, ROut>(
layer: Layer<ROut, E, RIn>,
scope: Scope,
build: (memoMap: MemoMap, scope: Scope) => Effect<Context<ROut>, E, RIn>,
) => Effect<Context<ROut>, E, RIn>;
}The variance interface for Layer type parameters.
Signature
interface Variance<in ROut, out E, out RIn> {
readonly "~effect/Layer": {
readonly _E: Covariant<E>;
readonly _RIn: Covariant<RIn>;
readonly _ROut: Contravariant<ROut>;
};
}Options
SpanOptions interface
Represents options that can be used to control the behavior of spans created for layers.
When to use
Use to configure tracing metadata, stack trace capture, and onEnd finalization for spans created by Layer.span and Layer.withSpan during layer construction.
Details
Extends Tracer.SpanOptions with onEnd, which runs when the layer span ends as the layer scope closes.
See
Signature
interface SpanOptions extends SpanOptions {
readonly onEnd?: (span: Span, exit: Exit<unknown, unknown>) => Effect<void>;
}Other
Signature
declare const catch: {
<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>;
}Providing Services
Feeds the output services of the dependency layer into the requirements of this layer, returning a layer that only provides the services from this layer.
When to use
Use when you need to hide an implementation dependency layer from callers.
Details
In serviceLayer.pipe(Layer.provide(dependencyLayer)), the dependency layer is built first and is used to satisfy the requirements of serviceLayer.
See
provideMergefor retaining the dependency services
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 [Any, ...Array<Any>]>(
that: Layers,
): <A, E, R>(
self: Layer<A, E, R>,
) => Layer<
A,
E | Error<Layers[number]>,
Services<Layers[number]> | Exclude<R, Success<Layers[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 [Any, ...Array<Any>]>(
self: Layer<A, E, R>,
that: Layers,
): Layer<
A,
E | Error<Layers[number]>,
Services<Layers[number]> | Exclude<R, Success<Layers[number]>>
>;
};provideMerge
Feeds the output services of the dependency layer into the requirements of this layer, returning a layer that provides both sets of services.
When to use
Use when you need to compose Layers while keeping both the constructed service and the dependency used to build it available.
Details
Prefer provide when the dependency should stay private.
See
providefor keeping dependency services private
Signature
declare const provideMerge: {
<RIn, E, ROut>(
that: Layer<ROut, E, RIn>,
): <RIn2, E2, ROut2>(
self: Layer<ROut2, E2, RIn2>,
) => Layer<ROut | ROut2, E | E2, RIn | Exclude<RIn2, ROut>>;
<Layers extends [Any, ...Array<Any>]>(
that: Layers,
): <A, E, R>(
self: Layer<A, E, R>,
) => Layer<
A | Success<Layers[number]>,
E | Error<Layers[number]>,
Services<Layers[number]> | Exclude<R, Success<Layers[number]>>
>;
<RIn2, E2, ROut2, RIn, E, ROut>(
self: Layer<ROut2, E2, RIn2>,
that: Layer<ROut, E, RIn>,
): Layer<ROut2 | ROut, E2 | E, RIn | Exclude<RIn2, ROut>>;
<A, E, R, Layers extends [Any, ...Array<Any>]>(
self: Layer<A, E, R>,
that: Layers,
): Layer<
A | Success<Layers[number]>,
E | Error<Layers[number]>,
Services<Layers[number]> | Exclude<R, Success<Layers[number]>>
>;
};updateService
Updates a service in the context with a new implementation.
When to use
Use to adapt or extend a service's behavior during the creation of a layer.
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.
Signature
declare const updateService: {
<I, A>(
service: Key<I, A>,
f: (a: NoInfer<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>,
service: Key<I, A>,
f: (a: NoInfer<A>) => A,
): Layer<A1, E1, R1 | I>;
};Sequencing
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>;
};Performs the specified effect if this layer succeeds.
When to use
Use to run an effectful observation after a layer has been built successfully, such as logging or metrics, without changing the services the layer provides.
Details
The callback receives the services produced by this layer. Its result is discarded, and the original layer output is preserved.
See
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, RIn | Exclude<RIn2, Scope>>;
<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 | Exclude<RIn2, Scope>>;
};Performs the specified effect when this layer fails with any cause.
When to use
Use to run diagnostics or reporting when layer construction fails and the full Cause is needed.
Details
The callback receives the layer's Cause, so it can inspect typed errors, defects, and interruption information. If the callback succeeds, the layer fails again with the original cause; if the callback fails, that failure is added to the layer's error type.
See
tapErrorfor observing only typed layer construction errorscatchCausefor recovering from a layer construction failure by switching to another layer
Signature
declare const tapCause: {
<E, XE, RIn2, E2, X>(
f: (cause: Cause<XE>) => Effect<X, E2, RIn2>,
): <RIn, ROut>(self: Layer<ROut, E, RIn>) => Layer<ROut, E | E2, RIn | Exclude<RIn2, Scope>>;
<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 | Exclude<RIn2, Scope>>;
};Performs the specified effect if this layer fails.
When to use
Use to run logging, metrics, or other effects when layer construction fails while preserving the original typed error.
Details
The callback receives the typed error. If the callback succeeds, the layer still fails with the original error; if the callback fails, that failure is added to the layer's error type.
See
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, RIn | Exclude<RIn2, Scope>>;
<RIn, E, XE, ROut, RIn2, E2, X>(
self: Layer<ROut, E, RIn>,
f: (e: XE) => Effect<X, E2, RIn2>,
): Layer<ROut, E | E2, RIn | Exclude<RIn2, Scope>>;
};Services
CurrentMemoMap
Context service for the current MemoMap used in layer construction.
When to use
Use when building custom layer operations that need to access the current memoization map from the fiber context.
Details
This service wraps a MemoMap as a Context.Service, making it available for dependency injection during layer construction.
See
MemoMapthe memoization map type wrapped by this service
Signature
declare class CurrentMemoMap extends Shape<"effect/Layer/CurrentMemoMap", MemoMap, this> {
constructor(_: never);
static forkOrCreate<Services>(self: Context<Services>): MemoMap;
}Testing
Creates a mock layer for testing purposes. You can provide a partial implementation of the service. Any missing members that are Effects, Streams, Channels, or functions returning them will fail with an unimplemented defect when used.
Details
Missing members are represented by a value that can be used as an Effect, Stream, Channel, or as a function returning an Effect. This lets the mock preserve the shape of common service methods while still failing loudly when an unimplemented member is exercised.
Signature
declare const mock: {
<I, S extends object>(
service: Key<I, S>,
): (
implementation: Simplify<
{ [K in string | number | symbol]: S[K] } & { [K in string | number | symbol]: S[K] }
>,
) => Layer<I>;
<I, S extends object>(
service: Key<I, S>,
implementation: NoInfer<
Simplify<
{ [K in string | number | symbol]: S[K] } & { [K in string | number | symbol]: S[K] }
>
>,
): Layer<I>;
};PartialEffectful type
A utility type for creating partial mocks of services in testing.
When to use
Use to type partial test service implementations where only exercised effectful members are stubbed.
Details
This type makes Effect, Stream, and Channel values and functions returning them optional, while keeping non-effectful properties required. This allows you to provide only the methods you need to test while leaving others unimplemented.
See
mockfor creating a mock layer from a partial service implementation
Signature
type PartialEffectful<A extends object> = Types.Simplify<
{ [K in keyof A]: A[K] } & { [K in keyof A]: A[K] }
>;Tracing
parentSpan
Constructs a layer that provides an existing span as the current parent span.
Details
The supplied span is made available through Tracer.ParentSpan for layers that are built with this layer. This API does not create, end, or close the span; the caller remains responsible for the span's lifetime.
Signature
declare function parentSpan(span: AnySpan): Layer<ParentSpan>;Constructs a new Layer which creates a span and registers it as the current parent span.
Details
This allows you to create a traced scope for layer construction, making all operations within the layer constructor part of the same trace span. The span is automatically ended when the layer's scope is closed. If onEnd is provided, it receives the span and the layer scope's exit value when the span ends.
Signature
declare function span(name: string, options?: SpanOptions): Layer<ParentSpan>;withParentSpan
Wraps a layer so spans created during its construction use the supplied span as their parent.
Details
Use this to attach layer construction to an existing trace hierarchy. This API does not create or end the supplied parent span.
When the supplied span is a native Span, layer construction also receives diagnostic information that helps associate failures with the layer call site. External spans are only installed as the parent span and do not add this diagnostic call-site information.
Signature
declare const withParentSpan: {
(
span: AnySpan,
options?: TraceOptions,
): <A, E, R>(self: Layer<A, E, R>) => Layer<A, E, Exclude<R, ParentSpan>>;
<A, E, R>(
self: Layer<A, E, R>,
span: AnySpan,
options?: TraceOptions,
): Layer<A, E, Exclude<R, ParentSpan>>;
};Wraps a Layer with a new tracing span, making all operations in the layer constructor part of the named trace span.
Details
This creates a new span for the layer's construction and execution. The span is automatically ended when the layer's scope is closed. This is useful for tracking the lifecycle and performance of layer initialization.
Signature
declare const withSpan: {
(
name: string,
options?: SpanOptions,
): <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,
): Layer<A, E, Exclude<R, ParentSpan>>;
};Utility Types
A type-level constraint for working with any Layer type.
When to use
Use to constrain generic parameters or layer collections to any Layer value while preserving its provided, error, and required service types for inference.
Details
This interface is used to constrain generic types to Layer values without specifying exact type parameters.
See
Signature
interface Any {
readonly "~effect/Layer": {
readonly _E: any;
readonly _RIn: any;
readonly _ROut: any;
};
}Extracts the error type (E) from a Layer type.
When to use
Use to derive a layer construction error type for helper types, wrappers, or APIs that preserve a layer failure channel.
See
Signature
type Error<T extends Any> = T extends Layer<infer _ROut, infer _E, infer _RIn> ? _E : never;satisfiesErrorType
Ensures that a layer's error type extends a given type E.
Details
This function provides compile-time type checking to ensure that the error type of a layer conforms to a specific type constraint.
Signature
declare function satisfiesErrorType<E>(): <ROut, E2, RIn>(
layer: Layer<ROut, E2, RIn>,
) => Layer<ROut, E2, RIn>;satisfiesServicesType
Ensures that a layer's requirements type extends a given type R.
Details
This function provides compile-time type checking to ensure that the requirements type of a layer conforms to a specific type constraint.
Signature
declare function satisfiesServicesType<RIn>(): <ROut, E, RIn2>(
layer: Layer<ROut, E, RIn2>,
) => Layer<ROut, E, RIn2>;satisfiesSuccessType
Ensures that a layer's success type extends a given type ROut.
Details
This function provides compile-time type checking to ensure that the success value of a layer conforms to a specific type constraint.
Signature
declare function satisfiesSuccessType<ROut>(): <ROut2, E, RIn>(
layer: Layer<ROut2, E, RIn>,
) => Layer<ROut2, E, RIn>;Extracts the service requirements (RIn) from a Layer type.
When to use
Use to derive the dependency requirements of a generic or inferred Layer without restating its RIn type parameter.
See
Signature
type Services<T extends Any> = T extends infer L
? L extends Layer<infer _ROut, infer _E, infer _RIn>
? _RIn
: never
: never;Extracts the service output type (ROut) from a Layer type.
When to use
Use to derive the services provided by an existing or generic Layer without restating its ROut type parameter.
See
Signature
type Success<T extends Any> = T extends Layer<infer _ROut, infer _E, infer _RIn> ? _ROut : never;Zipping
Merges this layer with another layer concurrently, producing a new layer with combined input, error, and output types.
When to use
Use to combine an existing Layer with another Layer or an array of layers while preserving pipeline style.
Details
This is a binary version of mergeAll that merges exactly two layers or one layer with an array of layers. The layers are built concurrently and their outputs are combined.
See
mergeAllfor merging several layers at once
Signature
declare const merge: {
<RIn, E, ROut>(
that: Layer<ROut, E, RIn>,
): <RIn2, E2, ROut2>(self: Layer<ROut2, E2, RIn2>) => Layer<ROut | ROut2, E | E2, RIn | RIn2>;
<Layers extends [Any, ...Array<Any>]>(
that: Layers,
): <A, E, R>(
self: Layer<A, E, R>,
) => Layer<A | Success<Layers[number]>, E | Error<Layers[number]>, R | Services<Layers[number]>>;
<RIn2, E2, ROut2, RIn, E, ROut>(
self: Layer<ROut2, E2, RIn2>,
that: Layer<ROut, E, RIn>,
): Layer<ROut2 | ROut, E2 | E, RIn2 | RIn>;
<A, E, R, Layers extends [Any, ...Array<Any>]>(
self: Layer<A, E, R>,
that: Layers,
): Layer<A | Success<Layers[number]>, E | Error<Layers[number]>, R | Services<Layers[number]>>;
};Combines all the provided layers concurrently, creating a new layer with merged input, error, and output types.
When to use
Use when you need to combine multiple independent layers.
Details
All layers are built concurrently, and their outputs are merged into a single layer.
If multiple merged layers depend on the same layer value, that dependency is shared by default. Reuse a named layer value when you want services to share the same resource, such as one database pool.
See
mergefor merging one layer with another layer or array
Signature
declare function mergeAll<
Layers extends [Layer<never, any, any>, ...Array<Layer<never, any, any>>],
>(
...layers: Layers
): Layer<Success<Layers[number]>, Error<Layers[number]>, Services<Layers[number]>>;
Constructs a layer from an effect that produces a single service.
When to use
Use when you need to construct a
Layer-provided service with anEffect, dependencies, or scoped resource acquisition.Details
This allows you to create a
Layerfrom anEffectthat produces a service. TheEffectis executed in the scope of the layer, allowing for proper resource management.See
effectContextfor effectfully providing multiple serviceseffectDiscardfor running construction work without providing services