Building Tiny Effects (Learning Effect, Part 2)

By Esteban Trillo2 min readupdated Learning EffectEffectTypeScript

A value goes in one bag, an error in the other. Both are just values.
A value goes in one bag, an error in the other. Both are just values.

The first Effect post gave me the shape:

Effect.Effect<Success, Error, Requirements>;

Now I want the smallest thing I can build with it.

No services. No architecture. No big theory.

Just success and failure.

The plain version

In plain TypeScript, success and failure take different roads.

const getName = (input: string): string => {
if (input === "") {
throw new Error("Name is required");
}
return input;
};

Success comes back through return.

Failure leaves through throw.

Only one of them shows up in the type.

Effect gives both of them a value you can hold.

A successful Effect

import { Effect } from "effect";
const answer = Effect.succeed(42);

The type is:

Effect.Effect<number, never, never>;

Read it slowly:

  • success: number
  • error: never
  • requirements: never

This Effect gives you a number. It cannot fail. It needs nothing.

What does Effect.succeed(42) put in the success slot?

A failed Effect

class MissingName {
readonly _tag = "MissingName";
}
const failed = Effect.fail(new MissingName());

The type is:

Effect.Effect<never, MissingName, never>;

That looks weird at first.

But it makes sense. This Effect does not produce a success value. It only fails.

So the success slot is never.

What does Effect.fail(new MissingName()) put in the error slot?

The pipe is just the path

Effect code often uses .pipe(...).

const program = Effect.succeed("trillo").pipe(
Effect.map((name) => name.toUpperCase()),
);

This reads as:

  1. start with "trillo"
  2. turn it into uppercase
  3. return a new Effect

The program still has not run.

It is still a description.

After mapping 'trillo' to uppercase, what is the success type?

The mistake: throwing inside an Effect

Old habits come back fast.

const getName = (input: string) =>
Effect.sync(() => {
if (input === "") {
throw new MissingName();
}
return input;
});

This compiles.

The type is Effect.Effect<string, never, never>.

The error slot says never. The code can still fail.

Effect treats that throw as a defect. A defect is a crash you did not plan for. It never shows up in the error slot.

How to spot it: a throw inside Effect.sync, next to an error slot that says never.

Return the failure instead:

const getName = (input: string) =>
input === "" ? Effect.fail(new MissingName()) : Effect.succeed(input);

Now the type is Effect.Effect<string, MissingName, never>.

The failure is back where you can see it.

What I am taking with me

succeed and fail are not magic.

They are just the two smallest Effect values:

Effect.succeed(value);
Effect.fail(error);

One fills the success slot.

One fills the error slot.

Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.