# Stop Throwing Errors with Effect (Learning Effect, Part 1)

- Published: 2026-07-10
- Updated: 2026-10-21
- Tags: Learning Effect, Effect, TypeScript
- URL: https://devtrillo.com/writing/learning-effect-1-introduction/

I want to learn Effect the annoying way: by using it, getting confused, and writing down the parts that finally click.

This is the first note.

Effect is a TypeScript library for writing programs where the scary parts are visible. A function can fail. It can need a database. It can do async work. Normal TypeScript hides a lot of that behind `Promise`, `throw`, and comments.

Effect puts those facts in the type.

Every example here is written against Effect 4 (version 4.0.5 when I checked). If you learned Effect 3, the ideas carry over. Only a few names changed.

```ts
Effect.Effect<Success, Error, Requirements>;
```

Read that as:

- what you get if it works
- what can go wrong
- what the code needs from the outside world

That is the whole first door.

## Start with a normal function

Here is the usual TypeScript version:

```ts
const parseUserId = (input: string): number => {
  const id = Number(input);

  if (!Number.isInteger(id)) {
    throw new Error("User id must be an integer");
  }

  return id;
};
```

The type says this returns a `number`.

It does not say it can throw.

So the caller sees this:

```ts
const id = parseUserId("abc");
```

And the crash is hiding off-screen.

**Quiz:** What is missing from the type of parseUserId(input: string): number?

- The function can fail
- The function is async
- The function needs a database

**Answer:** The function can fail — The return type only says number. It does not tell the caller that bad input throws an error.

## The Effect shape

With Effect, failure becomes part of the return value.

```ts
import { Effect } from "effect";

class InvalidUserId {
  readonly _tag = "InvalidUserId";
  constructor(readonly input: string) {}
}

const parseUserId = (input: string) =>
  Effect.sync(() => Number(input)).pipe(
    Effect.flatMap((id) =>
      Number.isInteger(id)
        ? Effect.succeed(id)
        : Effect.fail(new InvalidUserId(input)),
    ),
  );
```

The important part is not the syntax yet. The important part is the type.

```ts
Effect.Effect<number, InvalidUserId, never>;
```

That says:

- success: `number`
- failure: `InvalidUserId`
- requirements: `never`

`never` means this effect does not need anything from the outside world. No database. No config. No logger. Nothing.

**Quiz:** In Effect.Effect<number, InvalidUserId, never>, what does never mean?

- The program never runs
- The effect has no external requirements
- The effect can never fail

**Answer:** The effect has no external requirements — The third slot is the environment. never means this effect does not need services from the outside.

## Effect values do not run by themselves

This part matters.

An Effect is a description of work. Creating one does not run it.

```ts
const program = parseUserId("123");
```

That line builds a value. It has not parsed anything yet.

To run it at the edge of your app, you use a runner:

```ts
const result = await Effect.runPromise(program);
```

This is different from calling an async function. With a `Promise`, the work usually starts right away. With Effect, you can build the program first, combine it with other work, handle errors, provide dependencies, and run it once at the edge.

**Quiz:** When does an Effect run?

- As soon as you create it
- Only when you pass it to a runner like Effect.runPromise
- Whenever TypeScript checks the type

**Answer:** Only when you pass it to a runner like Effect.runPromise — An Effect is a value that describes work. A runner starts the work.

## Install it

Effect is one package.

```sh
npm install effect
```

It wants TypeScript with `strict` turned on.

Every example in this series imports from the same place:

```ts
import { Effect } from "effect";
```

## A first program you can run

Put this in `main.ts`:

```ts
import { Effect } from "effect";

class InvalidUserId {
  readonly _tag = "InvalidUserId";
  constructor(readonly input: string) {}
}

const parseUserId = (input: string) =>
  Effect.sync(() => Number(input)).pipe(
    Effect.flatMap((id) =>
      Number.isInteger(id)
        ? Effect.succeed(id)
        : Effect.fail(new InvalidUserId(input)),
    ),
  );

const program = parseUserId("123");

Effect.runPromise(program).then(console.log);
```

Run it:

```sh
npx tsx main.ts
```

It prints `123`.

Now change `"123"` to `"abc"` and run it again.

The promise rejects. The output names `InvalidUserId` and the input that caused it.

Nothing ran until the last line.

## Why bother?

For this tiny example, you probably would not.

A plain function with a union return is enough:

```ts
type ParseResult =
  { ok: true; value: number } | { ok: false; error: "InvalidUserId" };
```

Effect starts earning its keep when the program has more than one kind of failure and more than one outside dependency.

For example:

- parse the user id
- fetch the user
- call a payment API
- retry only the network error
- log the failure with request context
- return a safe error to the client

That is where `try/catch` starts to smear across the code. Effect gives you one pipe where success, failure, retry, timeout, and dependencies stay visible.

> **Note:** My rule for now: do not reach for Effect to parse one number. Reach for it
> when the boring error path is already bigger than the happy path.

## The part I am taking with me

The first mental model is simple:

```ts
Effect.Effect<Success, Error, Requirements>;
```

If I can read those three slots, I can start reading Effect code.

Next I want to learn the pipe style: `succeed`, `fail`, `flatMap`, and why Effect code often reads inside-out until it clicks
