<const C extends StoreContractValue>(
scope: string | StoreScopeTag,
contract: C
): StoreEffectsOf<C>Build a PURE object of effects from a contract — the HandleOf structure (nested shape tree +
custom methods) where each method is (...args) => resolveOrDie(scope, contract).flatMap((handle) =>
handle.<path>(...args)). So every method carries Storage in its requirement (see StoreEffectsOf),
the error channel stays clean (resolveOrDie dies on an unregistered custom store rather than
surfacing StoreScopeNotRegistered), and there is no resolution / yield* / memo cell here — the
handle memo lives in the storage bridge's .at. No error handling (catchWriteErrors owns that).
Write methods honestly carry StoreWriteError in their error channel (a journal/IO write failure); catchWriteErrors narrows it out. Reads carry no error.
export const const effects: <
C extends StoreContractValue
>(
scope: string | StoreScopeTag,
contract: C
) => StoreEffectsOf<C>
Build a PURE object of effects from a contract — the
HandleOf
structure (nested shape tree +
custom methods) where each method is (...args) => resolveOrDie(scope, contract).flatMap((handle) =>
handle.<path>(...args)). So every method carries Storage in its requirement (see
StoreEffectsOf
),
the error channel stays clean (
resolveOrDie
dies on an unregistered custom store rather than
surfacing StoreScopeNotRegistered), and there is no resolution / yield* / memo cell here — the
handle memo lives in the storage bridge's .at. No error handling (
catchWriteErrors
owns that).
Write methods honestly carry
StoreWriteError
in their error channel (a journal/IO write
failure);
catchWriteErrors
narrows it out. Reads carry no error.
effects = <const function (type parameter) C in <const C extends StoreContractValue>(scope: string | StoreScopeTag, contract: C): StoreEffectsOf<C>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): StoreEffectsOf<C>C,
): type StoreEffectsOf<
C extends StoreContractValue
> = any
The object of effects produced by
effects
: the
HandleOf
structure (nested shape tree +
custom methods) with
Storage
added to every method's requirement channel, carrying the
StoreEffectsVariance
brand.
StoreEffectsOf<function (type parameter) C in <const C extends StoreContractValue>(scope: string | StoreScopeTag, contract: C): StoreEffectsOf<C>C> => {
const const method: (
path: string
) => (
...args: ReadonlyArray<unknown>
) => Effect.Effect<unknown, never, Storage>
method =
(path: stringpath: string) =>
(...args: readonly unknown[]args: interface ReadonlyArray<T>ReadonlyArray<unknown>) =>
const resolveOrDie: <
C extends StoreContractValue
>(
scope: string | StoreScopeTag,
contract: C
) => Effect.Effect<
StoreHandleOf<C>,
never,
Storage
>
Like
resolve
, but guarantees a handle (resolve hardened with orDie). With the baked-in
default store in context (it materializes any scope on demand), this never fails — the always-on
observability path, where a resource's engine records unconditionally with no service-sniffing. If a
custom store is in context and lacks this scope, that's a wiring error and it dies with a clear
message (bake the default so it can materialize the scope).
resolveOrDie(scope: anyscope, contract: const C extends StoreContractValuecontract).Pipeable.pipe<Effect.Effect<StoreHandleOf<C>, never, Storage>, Effect.Effect<unknown, never, Storage>>(this: Effect.Effect<StoreHandleOf<C>, never, Storage>, ab: (_: Effect.Effect<StoreHandleOf<C>, never, Storage>) => Effect.Effect<unknown, never, Storage>): Effect.Effect<unknown, never, Storage> (+21 overloads)pipe(
import EffectEffect.const flatMap: <any, unknown, never, never>(f: (a: any) => Effect.Effect<unknown, never, never>) => <E, R>(self: Effect.Effect<any, E, R>) => Effect.Effect<unknown, E, R> (+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((handle: anyhandle) => const callAt: (
handle: unknown,
path: string,
args: ReadonlyArray<unknown>
) => Effect.Effect<unknown>
Navigate a resolved handle to the (possibly dotted) method at path and apply it.
callAt(handle: anyhandle, path: stringpath, args: readonly unknown[]args)),
);
const const flat: Record<string, unknown>flat: type Record<K extends keyof any, T> = {
[P in K]: T
}
Construct a type with a set of properties K of type T
Record<string, unknown> = {};
for (const const shapeKey: anyshapeKey of import shapeRowsByKeyshapeRowsByKey(contract: const C extends StoreContractValuecontract.shapes).keys()) {
const flat: Record<string, unknown>flat[`${const shapeKey: anyshapeKey}.append`] = const method: (
path: string
) => (
...args: ReadonlyArray<unknown>
) => Effect.Effect<unknown, never, Storage>
method(`${const shapeKey: anyshapeKey}.append`);
const flat: Record<string, unknown>flat[`${const shapeKey: anyshapeKey}.read`] = const method: (
path: string
) => (
...args: ReadonlyArray<unknown>
) => Effect.Effect<unknown, never, Storage>
method(`${const shapeKey: anyshapeKey}.read`);
}
for (const const name: stringname of var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.keys(o: {}): string[] (+1 overload)Returns the names of the enumerable string properties and methods of an object.
keys(contract: const C extends StoreContractValuecontract.customEntries)) {
const flat: Record<string, unknown>flat[const name: stringname] = const method: (
path: string
) => (
...args: ReadonlyArray<unknown>
) => Effect.Effect<unknown, never, Storage>
method(const name: stringname);
}
const const built: anybuilt = import nestHandlenestHandle(const flat: Record<string, unknown>flat);
const stampEffectsBrand: (
target: object
) => void
Stamp the honest (present-at-runtime)
TypeId
brand so
isStoreEffects
and the
mapEffects
/
catchWriteErrors
constraint are backed by a real property, not a phantom.
Non-enumerable so it stays invisible to method access / destructuring / the
flattenEffects
walk.
stampEffectsBrand(const built: anybuilt);
// Same generic-object structural-rebuild idiom as `makeShapeHandles` / `makeShapeRefs`: the effects
// object is assembled by dynamic assignment (then nested), so its type is asserted once here.
return const built: anybuilt as type StoreEffectsOf<
C extends StoreContractValue
> = any
The object of effects produced by
effects
: the
HandleOf
structure (nested shape tree +
custom methods) with
Storage
added to every method's requirement channel, carrying the
StoreEffectsVariance
brand.
StoreEffectsOf<function (type parameter) C in <const C extends StoreContractValue>(scope: string | StoreScopeTag, contract: C): StoreEffectsOf<C>C>;
};