<const C extends StoreContractValue>(
scope: string | StoreScopeTag,
contract: C
): Effect.Effect<StoreHandleOf<C>, StoreScopeNotRegistered, Storage>Resolve the store handle for a scope from the storage in context (an app Service, or the
baked-in in-memory default). Collapses the flatMap(bridge, (b) => b.at(scope, contract)) plumbing
so consumers never touch the underlying service directly.
Fails StoreScopeNotRegistered when the provided storage doesn't carry this scope — the opt-in path (e.g. persist only if the app wired durable storage for me). For the always-on observability path, use resolveOrDie.
export const const resolve: <
C extends StoreContractValue
>(
scope: string | StoreScopeTag,
contract: C
) => Effect.Effect<
StoreHandleOf<C>,
StoreScopeNotRegistered,
Storage
>
Resolve the store handle for a scope from the storage in context (an app
Service
, or the
baked-in in-memory default). Collapses the flatMap(bridge, (b) => b.at(scope, contract)) plumbing
so consumers never touch the underlying service directly.
Fails
StoreScopeNotRegistered
when the provided storage doesn't carry this scope — the
opt-in path (e.g. persist only if the app wired durable storage for me). For the always-on
observability path, use
resolveOrDie
.
resolve = <const function (type parameter) C in <const C extends StoreContractValue>(scope: string | StoreScopeTag, contract: C): Effect.Effect<StoreHandleOf<C>, StoreScopeNotRegistered, Storage>C extends import StoreContractValueStoreContractValue>(
scope: anyscope: string | import StoreScopeTagStoreScopeTag,
contract: const C extends StoreContractValuecontract: function (type parameter) C in <const C extends StoreContractValue>(scope: string | StoreScopeTag, contract: C): Effect.Effect<StoreHandleOf<C>, StoreScopeNotRegistered, Storage>C,
): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<import StoreHandleOfStoreHandleOf<function (type parameter) C in <const C extends StoreContractValue>(scope: string | StoreScopeTag, contract: C): Effect.Effect<StoreHandleOf<C>, StoreScopeNotRegistered, Storage>C>, import StoreScopeNotRegisteredStoreScopeNotRegistered, class Storageclass Storage {
Service: Service;
key: Identifier;
}
Scope bridge every store handle resolves through — provided by an app
Service
layer or
the soft-default
layerDefaultMemory
(via
withDefaultStorage
). Engines resolve
handles via
withDefault
/
withStorage
(preferred) or bridge.at.
Storage> =>
import EffectEffect.const flatMap: <StorageApi, never, Storage, StoreHandleOf<C>, StoreScopeNotRegistered, Storage>(self: Effect.Effect<StorageApi, never, Storage>, f: (a: StorageApi) => Effect.Effect<StoreHandleOf<C>, StoreScopeNotRegistered, Storage>) => Effect.Effect<StoreHandleOf<C>, any, Storage> (+1 overload)Chains effects to produce new Effect instances, useful for combining
operations that depend on previous results.
When to use
Use when you need to chain multiple effects, ensuring that each
step produces a new Effect while flattening any nested effects that may
occur.
Details
flatMap lets you sequence effects so that the result of one effect can be
used in the next step. It is similar to flatMap used with arrays but works
specifically with Effect instances, allowing you to avoid deeply nested
effect structures.
Since effects are immutable, flatMap always returns a new effect instead of
changing the original one.
Example (Choosing flatMap syntax variants)
import { Effect, pipe } from "effect"
const myEffect = Effect.succeed(1)
const transformation = (n: number) => Effect.succeed(n + 1)
const flatMappedWithPipe = pipe(myEffect, Effect.flatMap(transformation))
const flatMappedWithDataFirst = Effect.flatMap(myEffect, transformation)
const flatMappedWithMethod = myEffect.pipe(Effect.flatMap(transformation))
Example (Sequencing dependent effects)
import { Data, Effect, pipe } from "effect"
class DiscountRateError extends Data.TaggedError("DiscountRateError")<{}> {}
// Function to apply a discount safely to a transaction amount
const applyDiscount = (
total: number,
discountRate: number
): Effect.Effect<number, DiscountRateError> =>
discountRate === 0
? Effect.fail(new DiscountRateError())
: Effect.succeed(total - (total * discountRate) / 100)
// Simulated asynchronous task to fetch a transaction amount from database
const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
// Chaining the fetch and discount application using `flatMap`
const finalAmount = pipe(
fetchTransactionAmount,
Effect.flatMap((amount) => applyDiscount(amount, 5))
)
Effect.runPromise(finalAmount).then(console.log)
// Output: 95
flatMap(class Storageclass Storage {
key: Identifier;
of: (this: void, self: StorageApi) => StorageApi;
context: (self: StorageApi) => Context.Context<Storage>;
use: (f: (service: StorageApi) => Effect.Effect<A, E, R>) => Effect.Effect<A, E, Storage | R>;
useSync: (f: (service: StorageApi) => A) => Effect.Effect<A, never, Storage>;
Identifier: Identifier;
Service: Shape;
stack: string | undefined;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
Scope bridge every store handle resolves through — provided by an app
Service
layer or
the soft-default
layerDefaultMemory
(via
withDefaultStorage
). Engines resolve
handles via
withDefault
/
withStorage
(preferred) or bridge.at.
Storage, (bridge: StorageApibridge) =>
bridge: StorageApibridge.at(typeof scope: anyscope === "string" ? scope: stringscope : scope: anyscope.key, contract: const C extends StoreContractValuecontract),
);