Either

Handle computations that might fail with explicit error handling

What is Either?

Either is a data type that represents a choice between two values: Left (conventionally used for errors) and Right (for success values). Unlike exceptions, Either makes error handling explicit and type-safe.

// Traditional error handling:
try { const user = getUser() } catch (e) { }

// Either approach:
const user: Either<Error, User> = getUser()
Left & Right
map
fold
chain
error handling

Explicit Error Handling

Make error cases visible in the type system

🛤️

Railway Pattern

Chain operations that short-circuit on error

Type-Safe

Both error and success types are known

🔗

Composable

Build complex flows from simple parts

Either in Action

❌ Traditional Approach

function divide(a: number, b: number): number {
  if (b === 0) {
    throw new Error('Division by zero')
  }
  return a / b
}

try {
  const result = divide(10, 0)
  console.log(result)
} catch (error) {
  console.error(error.message)
}

✅ Either Approach

function divide(a: number, b: number): Either<string, number> {
  return b === 0
    ? E.left('Division by zero')
    : E.right(a / b)
}

pipe(
  divide(10, 0),
  E.fold(
    error => console.error(error),
    result => console.log(result)
  )
) // Errors are values!

Practice Exercises

1

Left And Right

Learn about left and right in fp-ts either

beginnerStart
2

Map

Learn about map in fp-ts either

beginnerStart
3

Fold

Learn about fold in fp-ts either

beginnerStart
4

Chain

Learn about chain in fp-ts either

intermediateStart
5

From Predicate

Learn about from predicate in fp-ts either

intermediateStart
6

Error Handling

Learn about error handling in fp-ts either

intermediateStart
7

Get Or Else

Learn about get or else in fp-ts either

advancedStart
8

Map Left

Learn about map left in fp-ts either

advancedStart

Why Learn Either?

Master Either

Learn the fundamental concepts and patterns that make Either powerful

💪

8 Exercises

Practice with hands-on exercises from beginner friendly level

🚀

Production Ready

Apply Either patterns to build robust, type-safe applications