Effect.asVoid

Effect.asVoid maps over a successful Effect, returning an Effect that returns void. This is similar to Effect.void (42) from the previous lesson, except in the form of an operator rather than a constructor.

A void success value helps consumers know that this Effectful operation is a command. Consider this example:

const authenticate = (form: LogInForm) =>
validateForm(form).pipe(
Effect.flatMap(() => Api.post("/v1/login", form)),
Effect.tap(() => Router.redirect("/")),
Effect.catchAll(() => Router.redirect("/access-denied")),
Effect.asVoid
);

It is clear that authenticating is an Effectful command. By using Effect.asVoid we make sure that consumers cannot use any of the details of the login API response.

This can be particularly helpful for library developers. Changing the success value of an Effect could be a breaking change, so returning void can guard against breaking changes.

We can implement Effect.asVoid using Effect.void (42) , or using other operators. Here are three equivalent implementations, using Effect.map (4) , Effect.flatMap (6) and Effect.zipRight (41) :

import { Effect } from "effect";
type AsVoid = <A, E, R>(
self: Effect.Effect<A, E, R>
) => Effect.Effect<void, E, R>;
// asVoid changes the success type only
const asVoid: AsVoid = (self) => Effect.map(self, () => undefined);
const asVoidFlatMap: AsVoid = (self) => Effect.flatMap(self, () => Effect.void);
// reminder: Effect.zipRight discards the success value of the left operator
const asVoidZip: AsVoid = (self) => Effect.zipRight(self, Effect.void);