What is Effect-ts and why is it needed?
\n\nEffect-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\nIn 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\nMain 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
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\nInstallation
\n\nTo install Effect-ts in your project, use npm or yarn:
\n\nnpm install effect\n\nor
\n\nyarn add effect\n\nAlso make sure you have TypeScript version 4.7 or higher installed:
\n\nnpm install typescript --save-dev\n\nTo work with Effect-ts, you will need to configure tsconfig.json by enabling strict mode (strict: true) and, preferably, target: "ES2020" or higher.
\n\nQuick start with code example
\n\nLet'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\nimport { 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\nIn 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
Main methods and functions of Effect-ts
\n\n1. Effect.sync
\nWhat it does: Creates an effect that wraps a synchronous function. The function is executed lazily (only when the effect is run).
\nParameters:
\n- \n
f: () => A— a synchronous function returning a value of type A. \n
Example:
\nimport { Effect } from "effect"\n\nc