onrails vs fp-ts
Concrete tagged unions vs typeclass-driven, HKT-encoded functional programming.
fp-ts brings the full vocabulary of typed functional programming to TypeScript —
Either, Option, Task, TaskEither, IO, Reader, and the typeclass
hierarchy (Functor, Monad, Applicative, Traversable) over a
higher-kinded-type encoding. onrails covers the same everyday cases —
fallible values and optional values — without the abstraction layer.
TL;DR
- fp-ts: maximally general. One set of combinators (
map,chain,sequence,traverse) that work across every type constructor via typeclasses + HKTs. Powerful, point-free, and a real learning curve. - onrails: concrete, monomorphic functions on two data types. No typeclasses, no
HKT encoding, no category-theory vocabulary — just
ResultandMaybe.
Same task, both libraries
// fp-ts — Either is error-LEFT, value-RIGHT
import * as from "fp-ts/Either";
import { } from "fp-ts/function";
const = (: string): .<, > =>
(
(), // Either<ParseError, Input>
.((): => ({ : "parse", : . })),
.(),
.(() => ({ ..., : () })),
);
const = (
(),
.(
() => (. === "validation" ? 422 : 400), // onLeft first
() => 201,
),
);// onrails — Result is value-FIRST: Result<T, E>
import * as from "@onrails/result";
const = (: string) =>
.(
(), // Result<Input, ParseError>
.((): => ({ : "parse", : . })),
.(), // Result<User, AppError>
.(() => ({ ..., : () })),
);
const = .((), () => 201, () => (. === "validation" ? 422 : 400));The shapes rhyme, but note two ergonomic differences:
- Type-parameter order. fp-ts
Either<E, A>puts the error first; onrailsResult<T, E>puts the value first (reads as "a number, or a string-error"). - Handler order. fp-ts
match(onLeft, onRight)takes the error handler first; onrailsmatch(value, onOk, onErr)takes the success handler first.
Key differences
No typeclasses, no HKTs
fp-ts's power comes from abstracting over the container: sequence,
traverse, and the Functor/Monad/Applicative instances are written once
and reused across Either, Option, Task, and your own types. The cost is
the HKT encoding (URI, Kind, module augmentation) and a vocabulary you have
to learn.
onrails declines that generality. There is no Functor<F> — there is map for
Result and map for Maybe, each a plain overloaded function. You lose
generic traverse; you gain a surface a TypeScript developer can read on day
one. For the common "collect a list of results" case, onrails gives you concrete
helpers instead:
import { combine, combineTuple } from "@onrails/result";
combine([ok(1), ok(2), ok(3)]); // Result<number[], E>
combineTuple([ok(1), ok("x")]); // Result<readonly [number, string], E>Built-in pattern matching
fp-ts collapses a sum type with the per-type fold/match for that type.
onrails ships a general matcher that narrows any discriminated union:
import { match } from "@onrails/pattern";
match(state)
.with({ status: "loading" }, (s) => `…${s.progress}%`)
.with({ status: "ready" }, (s) => s.data)
.exhaustive();Async
fp-ts models async as TaskEither<E, A> (a () => Promise<Either<E, A>>) with
its own combinator set. onrails uses ResultAsync<T, E>, a thenable carrier with
the same method names as the sync side (map, flatMap, recover, match) —
and it interops with native promises through tryAsync / fromPromise.
Choose fp-ts when
- You want generic, reusable combinators across many type constructors
(
traverse,sequence,Applicativevalidation, custom monads). - Your team already speaks the typeclass/category-theory vocabulary and wants maximal point-free composition.
Choose onrails when
- You want the everyday 90% — fallible and optional values — with a concrete, approachable API and no HKT machinery.
- You prefer value-first
Result<T, E>and success-first handlers. - You want pattern matching and
Maybein the same lightweight, tree-shakeable package.
fp-ts is now in maintenance; its ideas have largely moved into Effect. If you want the full functional platform, see onrails vs Effect.