11 KiB
Remove auto-flattening
Summary
Remove runtime isEffect auto-flattening from all public APIs. Callbacks must
return Effect explicitly; plain values are no longer silently wrapped or
ignored at runtime.
Background
Many functions across Effect, Schedule, Stream, Sink, and Channel accept
callbacks whose return type is X | Effect<X>. At runtime they call isEffect
to decide whether to flatten or wrap the result. This creates:
- Inconsistency —
Effect.tapauto-flattens butEffect.tapError,Effect.tapCause, etc. do not (issue #849). - Hidden behavior — a callback returning a non-Effect value (e.g.
Promise) is silently discarded or wrapped, masking bugs. - Type complexity — conditional types like
[X] extends [Effect<…>] ? … : …inflate overload surfaces.
The fix is to require Effect everywhere and remove the runtime dispatch.
Goals
- Remove all runtime
isEffectvalue-flattening checks from callback-based APIs. - Simplify type signatures: remove conditional-type overloads and
| Effectunions on callback return types. - Remove the
{ onlyEffect: true }option fromEffect.tap(it becomes the only behavior). - Keep
dual()discrimination uses ofisEffect— those distinguish data-first from data-last and are unrelated to auto-flattening.
Non-goals
- No changes to
Effect.fngenerator-vs-Effect discrimination (it serves a different purpose: supporting bothgeniterators and plain Effect returns). - No changes to
dual()discrimination logic that usesisEffectto detect data-first vs data-last invocations.
Affected functions
Effect module
| Function | Current callback return type | Change |
|---|---|---|
andThen |
X (conditional type resolves Effect<X> or Effect<X, …>) |
Require Effect<B, E2, R2> return; remove conditional-type overloads and NotFunction<X> overloads |
tap |
X (conditional type) + { onlyEffect: true } overloads |
Require Effect<B, E2, R2> return; remove conditional-type, NotFunction<X>, and onlyEffect overloads |
onExit |
Effect<void, XE, XR> | void |
Require Effect<void, XE, XR> |
onExitInterruptible |
Effect<void, XE, XR> | void |
Require Effect<void, XE, XR> |
onInterrupt |
Effect<void> | ((interruptors) => Effect<void>) |
Require (interruptors) => Effect<void>; drop the bare-Effect overload |
when |
LazyArg<boolean> | Effect<boolean> |
Require Effect<boolean> only |
acquireUseRelease |
Internal impl types release as Effect<void> | void |
Align internal impl with public sig (already Effect<void>) |
Schedule module
| Function | Current callback return type | Change |
|---|---|---|
addDelay |
DurationInput | Effect<DurationInput> |
Require Effect<DurationInput> |
map |
Output2 | Effect<Output2> |
Require Effect<Output2> |
modifyDelay |
DurationInput | Effect<DurationInput> |
Require Effect<DurationInput> |
reduce |
State | Effect<State> |
Require Effect<State> |
unfold |
State | Effect<State> |
Require Effect<State> |
while |
boolean | Effect<boolean> |
Require Effect<boolean> |
Stream module
| Function | Current callback return type | Change |
|---|---|---|
paginate |
[A, Option<S>] | Effect<[A, Option<S>]> |
Require Effect<[A, Option<S>]> |
when |
LazyArg<boolean> | Effect<boolean> |
Require Effect<boolean> |
Sink module
| Function | Current callback return type | Change |
|---|---|---|
fold |
S | Effect<S> |
Require Effect<S> |
foldArray |
S | Effect<S> |
Require Effect<S> |
foldUntil |
S | Effect<S> |
Require Effect<S> |
Channel module
| Function | Current callback return type | Change |
|---|---|---|
asyncQueue (private) |
void | Effect<unknown> |
Require Effect<void> |
mapInput |
InElem | Effect<InElem> |
Require Effect<InElem> |
mapInputError |
InErr | Effect<InErr> |
Require Effect<InErr> |
Internal
| Function | Current callback return type | Change |
|---|---|---|
request (internal) |
RequestResolver | Effect<RequestResolver> |
Require Effect<RequestResolver> |
Requirements
Effect.andThen
Type signature — before:
export const andThen: {
<A, X>(
f: (a: A) => X
): <E, R>(
self: Effect<A, E, R>
) => [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1> : Effect<X, E, R>
<X>(
f: NotFunction<X>
): <A, E, R>(
self: Effect<A, E, R>
) => [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1> : Effect<X, E, R>
// + data-first variants
}
Type signature — after:
export const andThen: {
<A, B, E2, R2>(f: (a: A) => Effect<B, E2, R2>): <E, R>(self: Effect<A, E, R>) => Effect<B, E | E2, R | R2>
<A, E, R, B, E2, R2>(self: Effect<A, E, R>, f: (a: A) => Effect<B, E2, R2>): Effect<B, E | E2, R | R2>
}
Runtime — before:
flatMap(self, (a) => {
if (isEffect(f)) return f
const value = typeof f === "function" ? internalCall(() => f(a)) : f
return isEffect(value) ? value : succeed(value)
})
Runtime — after:
flatMap(self, (a) => internalCall(() => f(a)))
Effect.tap
Type signature — after:
export const tap: {
<A, B, E2, R2>(f: (a: NoInfer<A>) => Effect<B, E2, R2>): <E, R>(self: Effect<A, E, R>) => Effect<A, E | E2, R | R2>
<A, E, R, B, E2, R2>(self: Effect<A, E, R>, f: (a: NoInfer<A>) => Effect<B, E2, R2>): Effect<A, E | E2, R | R2>
}
Remove NotFunction<X> overloads, conditional-type overloads, and
{ onlyEffect: true } overloads.
Runtime — after:
flatMap(self, (a) => as(internalCall(() => f(a)), a))
Effect.onExit / Effect.onExitInterruptible
Remove | void from the finalizer return type. The onExitPrimitive
implementation drops the isEffect(eff) ? flatMap(eff, …) : exit branch and
always flatMaps.
Effect.onInterrupt
Remove the bare-Effect overload. Always require a function
(interruptors: ReadonlySet<number>) => Effect<void, XE, XR>. Drop the
isEffect(finalizer) ? constant(finalizer) : finalizer dispatch.
Effect.when
Remove LazyArg<boolean> from the union. Require Effect<boolean, E2, R2>.
Drop the isEffect(condition) ? condition : sync(condition) dispatch.
Schedule, Stream, Sink, Channel functions
For each function listed in the tables above:
- Remove the
| Effect<…>or| plainValueunion from the callback return type, keeping onlyEffect<…>. - Remove the runtime
isEffect(result)branch — always treat the return as anEffect. - Update internal callers to wrap plain values in
Effect.succeedorEffect.syncwhere needed.
Migration
Callers currently relying on auto-flattening must wrap return values:
| Before | After |
|---|---|
Effect.andThen(self, 42) |
Effect.andThen(self, () => Effect.succeed(42)) |
Effect.andThen(self, (a) => a + 1) |
Effect.andThen(self, (a) => Effect.succeed(a + 1)) |
Effect.tap(self, (a) => console.log(a)) |
Effect.tap(self, (a) => Effect.sync(() => console.log(a))) |
Effect.tap(self, Console.log("hi")) |
Effect.tap(self, () => Console.log("hi")) |
Effect.tap(self, f, { onlyEffect: true }) |
Effect.tap(self, f) |
Effect.when(self, () => true) |
Effect.when(self, Effect.succeed(true)) |
Effect.onExit(self, (exit) => { … }) |
Effect.onExit(self, (exit) => Effect.sync(() => { … })) |
Schedule.addDelay(sched, () => "1 second") |
Schedule.addDelay(sched, () => Effect.succeed("1 second")) |
Sink.fold(zero, cont, (s, i) => s + i) |
Sink.fold(zero, cont, (s, i) => Effect.succeed(s + i)) |
Implementation order
Effect.andThenandEffect.tap— highest visibility; update type signatures, remove conditional-type overloads, simplify runtime.Effect.onExit,Effect.onExitInterruptible,Effect.acquireUseRelease— remove| voidfrom finalizer types andisEffectbranches inonExitPrimitive.Effect.onInterrupt— require function form only.Effect.when— requireEffect<boolean>.- Schedule functions —
addDelay,map,modifyDelay,reduce,unfold,while. - Stream functions —
paginate,when. - Sink functions —
fold,foldArray,foldUntil. - Channel functions —
mapInput,mapInputError,asyncQueue. - Internal —
request. - Fix all internal callers and tests that relied on auto-flattening.
Testing
- Update existing tests that pass plain values to auto-flattening APIs.
- Verify that passing a non-Effect to
andThen/tapis a compile error. - Run
pnpm testfor each affected package after changes.
Validation
pnpm lint-fixpnpm test <relevant test files>pnpm check(runpnpm cleanthen re-run if it fails)pnpm buildpnpm docgen