In this article, we review Effect. You will learn:
What is Effect?
Usage examples.
What is Effect?
Effect is a TypeScript library for building production-grade software: typed error handling, structured concurrency, resource safety, and observability, all from one composable core.
Why Effect? I read thru this Welcome section in the Effect docs and it describes that TypeScript is excellent at describing your data, but it says almost nothing about your programs: a function’s signature doesn’t tell you what can fail, what dependencies it needs, or whether it can be safely retried, timed out, or interrupted. Effect closes that gap with a single building block: the Effect type, a value that fully describes a program, including its success value, its possible errors, and its requirements.
Learn more about how Effect is useful.
Usage examples
Follow the installation steps provided. In the learning path section, Creating effects explains how you can define the success and failure cases in a function.
Traditionally, we would do the following to handle errors in a TS function:
// Type signature doesn't show possible exceptions
const divide = (a: number, b: number): number => {
if (b === 0) {
throw new Error("Cannot divide by zero")
}
return a / b
}
divide(4, 2) // => 2
but this function’s type signature do not indicate that they can throw exceptions. Now with Effect, you could rewrite this division function as shown below:
import { Effect } from "effect"
const divide = (a: number, b: number): Effect.Effect<number, Error> =>
b === 0
? Effect.fail(new Error("Cannot divide by zero"))
: Effect.succeed(a / b)
Effect.runSync(divide(4, 2)) // => 2
In this example, the divide function indicates in its return type Effect that the operation can either succeed with a number or fail with an Error.
┌─── Produces a value of type number
│ ┌─── Fails with an Error
▼ ▼
Effect<number, Error>
Below is another example, simulating a user retrieval operation:
import { Effect } from "effect"
// Define a User type
interface User {
readonly id: number
readonly name: string
}
// A mocked function to simulate fetching a user from a database
const getUser = (userId: number): Effect.Effect<User, Error> => {
// Normally, you would access a database or API here, but we'll mock it
const userDatabase: Record<number, User> = {
1: { id: 1, name: "John Doe" },
2: { id: 2, name: "Jane Smith" },
}
// Check if the user exists in our "database" and return appropriately
const user = userDatabase[userId]
if (user) {
return Effect.succeed(user)
} else {
return Effect.fail(new Error("User not found"))
}
}
// When executed, this will successfully return the user with id 1
const exampleUserEffect = getUser(1)
Effect.runSync(exampleUserEffect).name // => "John Doe"
About me:
Hey, my name is Ramu Narasinga. Email: ramu.narasinga@gmail.com
I spent 3+ years studying OSS codebases and wrote 400+ articles on what makes the production-grade. Now I'm putting that into practice differently - instead of writing every fix myself, I run coding agents that do it.
How it works? Register your machine as a Runtime, point it at your repo. Agents pick up issues. write the fix, open the PR. You just review, they execute.
Build your coding agents and get more work done in less time at thinkthroo.com

Top comments (0)