Effect-TS Has a Free API: TypeScript's Missing Standard Library for Production Apps

Published: (March 29, 2026 at 10:09 AM EDT)
3 min read
Source: Dev.to

Source: Dev.to

Overview

Effect is a TypeScript library that provides a standard library for production applications. It offers:

  • Typed errors (know exactly what can fail)
  • Dependency injection (no container needed)
  • Structured concurrency (fibers, semaphores)
  • Retry policies with backoff
  • Schema validation (similar to Zod but integrated)
  • Streams (reactive data processing)
  • Built‑in tracing and metrics

TypeScript lacks a standard way to handle typed errors, dependency injection, retries, rate limiting, or structured concurrency. Effect fills these gaps with a composable, type‑safe API.

Installation

npm install effect

Basic Usage

import { Effect, Console } from "effect";

const program = Effect.gen(function* () {
  yield* Console.log("Hello from Effect!");
  const result = yield* Effect.succeed(42);
  yield* Console.log(`Result: ${result}`);
});

Effect.runPromise(program);

Typed Errors

import { Effect, Data } from "effect";

class NotFoundError extends Data.TaggedError("NotFoundError") {}

class DatabaseError extends Data.TaggedError("DatabaseError") {}

const findUser = (id: string): Effect.Effect =>
  Effect.gen(function* () {
    const user = yield* queryDatabase(id);
    if (!user) {
      return yield* new NotFoundError({ id });
    }
    return user;
  });

// Handle specific errors
const program = findUser("123").pipe(
  Effect.catchTag("NotFoundError", (e) =>
    Effect.succeed({ name: "Default", id: e.id })
  ),
  Effect.catchTag("DatabaseError", (e) =>
    Effect.fail(new Error(`DB failed: ${e.cause}`))
  ),
);

Dependency Injection with Context & Layers

import { Effect, Context, Layer } from "effect";

class Database extends Context.Tag("Database") Effect.Effect }
>() {}

class Logger extends Context.Tag("Logger") Effect.Effect }
>() {}

const findUsers = Effect.gen(function* () {
  const db = yield* Database;
  const logger = yield* Logger;
  yield* logger.log("Fetching users...");
  return yield* db.query("SELECT * FROM users");
});

// Provide implementations
const DatabaseLive = Layer.succeed(Database, {
  query: (sql) => Effect.succeed([{ id: 1, name: "Alice" }]),
});

const LoggerLive = Layer.succeed(Logger, {
  log: (msg) => Effect.sync(() => console.log(msg)),
});

const MainLive = Layer.merge(DatabaseLive, LoggerLive);

Effect.runPromise(
  findUsers.pipe(Effect.provide(MainLive))
);

Retries, Scheduling, and Concurrency

import { Effect, Schedule } from "effect";

const fetchData = Effect.tryPromise(() =>
  fetch("https://api.example.com/data").then((r) => r.json())
);

// Retry with exponential backoff (up to 5 attempts)
const reliable = fetchData.pipe(
  Effect.retry(
    Schedule.exponential("1 second").pipe(
      Schedule.compose(Schedule.recurs(5))
    )
  ),
);

Running Tasks Concurrently

const tasks = [fetchUser(1), fetchUser(2), fetchUser(3)];

// Run all concurrently (max 3 at a time)
const results = yield* Effect.all(tasks, { concurrency: 3 });

Limiting Concurrency with a Semaphore

// Create a semaphore that allows up to 5 concurrent permits
const semaphore = yield* Effect.makeSemaphore(5);

// Wrap each task to acquire a permit before execution
const limited = tasks.map((task) => semaphore.withPermits(1)(task));
const results2 = yield* Effect.all(limited);

Resources

Feel free to explore the library for building robust, production‑grade TypeScript applications.

0 views
Back to Blog

Related posts

Read more »