Quick reference for fp-ts Either: create, transform, extract values for typed error handling, validation, and chaining failable operations in TypeScript.
From antigravity-awesome-skillsnpx claudepluginhub sickn33/antigravity-awesome-skills --plugin antigravity-awesome-skillsThis skill uses the workspace's default tool permissions.
Designs and optimizes AI agent action spaces, tool definitions, observation formats, error recovery, and context for higher task completion rates.
Enables AI agents to execute x402 payments with per-task budgets, spending controls, and non-custodial wallets via MCP tools. Use when agents pay for APIs, services, or other agents.
Compares coding agents like Claude Code and Aider on custom YAML-defined codebase tasks using git worktrees, measuring pass rate, cost, time, and consistency.
Either = success or failure. Right(value) or Left(error).
import * as E from 'fp-ts/Either'
E.right(value) // Success
E.left(error) // Failure
E.fromNullable(err)(x) // null → Left(err), else Right(x)
E.tryCatch(fn, toError) // try/catch → Either
E.map(fn) // Transform Right value
E.mapLeft(fn) // Transform Left error
E.flatMap(fn) // Chain (fn returns Either)
E.filterOrElse(pred, toErr) // Right → Left if pred fails
E.getOrElse(err => default) // Get Right or default
E.match(onLeft, onRight) // Pattern match
E.toUnion(either) // E | A (loses type info)
import { pipe } from 'fp-ts/function'
import * as E from 'fp-ts/Either'
// Validation
const validateEmail = (s: string): E.Either<string, string> =>
s.includes('@') ? E.right(s) : E.left('Invalid email')
// Chain validations (stops at first error)
pipe(
E.right({ email: 'test@example.com', age: 25 }),
E.flatMap(d => pipe(validateEmail(d.email), E.map(() => d))),
E.flatMap(d => d.age >= 18 ? E.right(d) : E.left('Must be 18+'))
)
// Convert throwing code
const parseJson = (s: string) => E.tryCatch(
() => JSON.parse(s),
(e) => `Parse error: ${e}`
)
// ❌ try/catch - errors not in types
try {
const data = JSON.parse(input)
process(data)
} catch (e) {
handleError(e)
}
// ✅ Either - errors explicit in types
pipe(
E.tryCatch(() => JSON.parse(input), String),
E.map(process),
E.match(handleError, identity)
)
Use Either when error type matters and you want to chain operations.