The Third Slot of Effect (Learning Effect, Part 7)

By Esteban Trillo2 min readupdated Learning EffectEffectTypeScript

The third drawer holds what it needs. It stays open until you provide it.
The third drawer holds what it needs. It stays open until you provide it.

The first two slots are easier to care about.

Effect.Effect<Success, Error, Requirements>;

Success is what I want.

Error is what can go wrong.

The third slot is quieter.

It says what the program needs.

The plain version

import { db } from "./db";
const fetchUser = async (id: number): Promise<User> => {
return db.users.findById(id);
};

The type is (id: number) => Promise<User>.

It does not mention db.

The dependency is real. It is not in the type.

To test this, you mock the module or point it at a real database.

The type gives you no hint either way.

In a big app, that is the hint you want most.

Which functions talk to the database? Today you search the imports to find out.

never means nothing is needed

Effect.Effect<number, InvalidUserId, never>;

This can parse a number and fail with InvalidUserId.

It does not need a database, config, logger, or HTTP client.

So the requirement is never.

What does never mean in the third slot?

A real dependency shows up there

interface UserRepo {
readonly findById: (id: number) => Effect.Effect<User, UserNotFound>;
}

A program that needs UserRepo has that in the type:

Effect.Effect<User, UserNotFound, UserRepo>;

Read it as:

I can give you a User, I may fail with UserNotFound, and I need a UserRepo.

In Effect.Effect<User, UserNotFound, UserRepo>, what must be provided before running?

Why this matters

In normal code, dependencies hide in imports.

A function calls a module. The module reads config. The config reads env vars. Somewhere in there, the test gets annoying.

Effect makes the need visible.

That does not magically make the design good.

But it makes the hidden part harder to ignore.

The mistake: running before you provide

The third slot is a checklist for the edge.

const program = fetchUser(1);
// Effect.Effect<User, UserNotFound, UserRepo>
Effect.runPromise(program);

This does not compile.

The error says UserRepo is not assignable to never.

That message reads strange the first time.

It means the runner wants an empty third slot. This program still needs something.

How to spot it: a type error that mentions never next to one of your service names.

The fix is to provide the service first.

Read the third slot before you run anything. It tells you what the edge still has to hand over.

That is the next part.

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.