A Practical Guide to Using Effect TS for TypeScript Development

Effect TS is a powerful TypeScript library that brings functional programming concepts to practical application development. It provides a robust way to handle side effects, errors, concurrency, and dependency injection in a type-safe manner. Instead of throwing exceptions or using callbacks, you model your program as a composable Effect value. This guide will walk you through the core concepts to get you productive quickly.

To start, install the library in your project with npm install effect or yarn add effect. Effect TS works seamlessly with TypeScript’s type system, so no extra configuration is needed.

Article illustration

Creating and Running Your First Effect

An Effect describes a computation that may produce a value, fail with an error, or require dependencies. The simplest effects are created with Effect.succeed and Effect.fail. To execute an effect, use Effect.runPromise (for async) or Effect.runSync (for synchronous).

Example

  • const hello = Effect.succeed("Hello, World!");
  • const result = await Effect.runPromise(hello); // “Hello, World!”
  • const fail = Effect.fail("Something went wrong");
  • try { await Effect.runPromise(fail) } catch (e) { console.log(e) }

Combining and Transforming Effects

Effect TS provides a rich set of combinators to chain and combine effects without nested callbacks. Use pipe (or traditional method calls) to apply transformations.

  • map – transforms a success value: Effect.succeed(1).pipe(Effect.map(n => n + 1))
  • flatMap – chain effects: Effect.succeed(2).pipe(Effect.flatMap(n => Effect.succeed(n * 3)))
  • zip – combine two effects into a tuple: Effect.zip(Effect.succeed("a"), Effect.succeed("b"))
  • all – run a list of effects in sequence: Effect.all([Effect.succeed(1), Effect.succeed(2)])

Error Handling

Instead of try/catch blocks, you handle errors declaratively. Use catchAll to provide a fallback effect, or orElse to try an alternative effect if the first fails. For example:

const safe = Effect.fail("Error").pipe(Effect.catchAll(err => Effect.succeed("Fallback")));

This makes error flows explicit and testable, and the types always reflect possible errors.

Dependency Injection with Effect TS

Effect TS natively supports dependency injection through Context and Service. You define a service as an interface, then provide an implementation when running the effect. This promotes loose coupling and easier testing. For instance:

  • Define a Logger tag: class Logger extends Context.Tag("Logger")<Logger, { info: (msg: string) => Effect<void, never, never> }>() {}
  • Create a layer that provides the service.
  • Use Effect.service(Logger) inside your effects.
  • Run with Effect.provideService or Layer.build.

Conclusion

Effect TS transforms the way you write TypeScript by making side effects, errors, and dependencies first-class citizens of your type system. Start small with simple effects, then gradually adopt combinators and dependency injection. The official documentation and community are excellent resources. Try replacing a few functions in your next project with Effect TS – you’ll quickly appreciate the clarity and safety it brings.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *