Effect-ts for TypeScript: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Effect-ts and why is it needed?

\n\n

Effect-ts is a powerful library for TypeScript that brings functional programming concepts to the language, allowing you to write clean, composable, and type-safe code. It solves one of the main problems in TypeScript (and JavaScript in general) development — managing side effects.

\n\n

In traditional imperative programming, side effects (working with networks, files, databases, generating random numbers, working with time) are scattered throughout the code, making it difficult to test, debug, and analyze. Effect-ts offers an elegant approach: all side effects are described as data (values) that can be combined, transformed, and executed in a controlled environment.

\n\n

Main advantages of Effect-ts:

\n
    \n
  • Type safety: The TypeScript compiler checks error types and successful results at the type level.
  • \n
  • Composability: Effects are easily combined using functions like map, flatMap, zip, and others.
  • \n
  • Error handling: Errors are handled as part of the effect type, not through exceptions.
  • \n
  • Testability: Dependencies (e.g., HTTP clients, databases) can be easily substituted.
  • \n
  • Concurrency: Built-in support for asynchrony, parallelism, and operation cancellation.
  • \n
  • Predictability: Code becomes more declarative and easier to understand.
  • \n
\n\n

Effect-ts is especially useful in large projects where reliability, testability, and scalability are important. It is used in backend development (Node.js), frontend (React), and even in mobile applications.

\n\n

Installation

\n\n

To install Effect-ts in your project, use npm or yarn:

\n\n
npm install effect
\n\n

or

\n\n
yarn add effect
\n\n

Also make sure you have TypeScript version 4.7 or higher installed:

\n\n
npm install typescript --save-dev
\n\n

To work with Effect-ts, you will need to configure tsconfig.json by enabling strict mode (strict: true) and, preferably, target: "ES2020" or higher.

\n\n

Quick start with code example

\n\n

Let's write a simple program that reads a user's name from the command line and outputs a greeting. This will show the basic concept of Effect-ts: describing an effect and executing it.

\n\n
import { Effect, Console } from "effect"\n\n// Describe the effect: ask for name\nconst askName: Effect.Effect<string> = Effect.sync(() => {\n  // In a real application, this would be keyboard input\n  return "Alexey"\n})\n\n// Describe the effect: output greeting\nconst greet = (name: string): Effect.Effect<void> =>\n  Console.log(`Hello, ${name}!`)\n\n// Combine effects\nconst program: Effect.Effect<void> = askName.pipe(\n  Effect.flatMap(greet)\n)\n\n// Run the program\nEffect.runSync(program)\n// Output: Hello, Alexey!
\n\n

In this example:

\n
    \n
  • Effect.sync — creates an effect from a synchronous function.
  • \n
  • Console.log — is an effect for console output.
  • \n
  • pipe — allows sequentially applying functions to a value.
  • \n
  • flatMap — used for sequential execution of effects.
  • \n
  • Effect.runSync — executes the effect synchronously (only for synchronous effects).
  • \n
\n\n

Main methods and functions of Effect-ts

\n\n

1. Effect.sync

\n

What it does: Creates an effect that wraps a synchronous function. The function is executed lazily (only when the effect is run).

\n

Parameters:

\n
    \n
  • f: () => A — a synchronous function returning a value of type A.
  • \n
\n

Example:

\n
import { Effect } from "effect"\n\nc                    

Recommendations