onrails vs neverthrow
The closest neighbour — same railway idea, different data model, plus Maybe and pattern matching.
neverthrow and onrails solve the same core problem — make expected failure a value, not an exception — and they look similar at the call site. onrails is close enough that it ships a neverthrow-compat tier and a codemod to move existing code over. The differences are in the data model and the surrounding surface.
Already on neverthrow?
You don't have to rewrite by hand — onrails ships a codemod and a neverthrow-compat tier to migrate existing code incrementally.
TL;DR
- neverthrow: a
Resultclass with fluent methods. Minimal and focused —Result/ResultAsynconly. - onrails:
Resultas a plain tagged union consumed by dual-form functions (or an optional fluent wrapper), plusMaybe, exhaustivematch, and a named-stepRailwaybuilder.
Same task, both libraries
// onrails — dual-form functions over a tagged union
import * as from "@onrails/result";
const = (: string) =>
.(
(), // Result<Input, ParseError>
.((): => ({ : "parse", : . })),
.(), // Result<User, AppError>
.(() => ({ ..., : () })),
);
const = .(
(),
() => 201,
() => (. === "validation" ? 422 : 400),
);// neverthrow — fluent methods on a Result instance
import { , , } from "neverthrow";
const = (: string): <, > =>
()
.((): => ({ : "parse", : . }))
.()
.(() => ({ ..., : () }));
const = ().(
() => 201,
() => (. === "validation" ? 422 : 400),
);Prefer dots? onrails has an opt-in fluent wrapper too:
import { fluent } from "@onrails/result/fluent";
fluent(parseJson(raw)).andThen(validate).map(normalize).match(onOk, onErr);Key differences
Data model: class hierarchy vs tagged data
A neverthrow Result is an instance of an Ok/Err class tree. An onrails
Result is a tagged object — { _tag: "Ok", value } / { _tag: "Err", error }
— whose own fields are plain data, with the methods on a non-enumerable
prototype. That means onrails values serialize to the tagged shape
(JSON.stringify, a queue), are inspectable, and directly
pattern-matchable. (Data that crossed JSON.parse / structuredClone /
postMessage has lost its methods; lift it with Result.from.)
import { match } from "@onrails/pattern";
match(result)
.with({ _tag: "Ok" }, (r) => use(r.value))
.with({ _tag: "Err" }, (r) => report(r.error))
.exhaustive();For the everyday Ok/Err fork you'd still reach for match(result, onOk, onErr)
from @onrails/result — the snippet above just shows the value is an ordinary
union your tools can see into, not a class you have to call methods on.
Beyond Result
neverthrow is Result-only. onrails adds:
Maybefor expected absence (Some/None) with the same dual-form API.@onrails/patternfor exhaustive, type-narrowed matching on any union.Railway— a named-step builder for 4+ step workflows with mixed sync/async IO.
The throwing escape hatch
neverthrow's _unsafeUnwrap() and onrails' unwrapOk/unwrapErr both throw on
the wrong branch. onrails treats these as a test/assertion tier and the
@onrails/eslint-plugin / @onrails/biome-plugin rules flag them outside
*.spec.ts / *.test.ts.
Migration is mechanical
@onrails/result/compat/neverthrow mirrors the neverthrow shape, so migration
is two mechanical stages — preview either with --dry first:
# stage 1 — rewrite neverthrow imports + package.json deps to the compat shim
bunx @onrails/codemod ./path/to/repo --dry
bunx @onrails/codemod ./path/to/repo
# stage 2 — rewrite compat-shim imports to canonical @onrails/result
bunx @onrails/codemod ./path/to/repo --to-native --dry
bunx @onrails/codemod ./path/to/repo --to-nativeStage 2 is intentionally conservative: it only rewrites imports that have a native equivalent, leaving compat-only shapes flagged for manual review.
Stage 1 only touches the import — your call sites are untouched. Drag the handle to see exactly what changes:
import { ok, err, Result } from "neverthrow";const parseConfig = (raw: string): Result<Config, ConfigError> =>ok(raw).andThen(decode).map(normalize);import { ok, err, Result } from "@onrails/result/compat/neverthrow";const parseConfig = (raw: string): Result<Config, ConfigError> =>ok(raw).andThen(decode).map(normalize);Performance
Microbenchmarks (runtime + fair import-graph size) are documented under
Benchmarks. Summary for this pairing: smaller
import graph for a comparable Result surface; sync map near parity; neverthrow
ahead on deep andThen; onrails ahead on combine and a short async chain in
the committed snapshot. Prefer core carriers over the compat shim on hot paths.
Choose neverthrow when
- You only need
Result, want the smallest possible surface, and prefer pure method chaining. - You don't need
Maybe, pattern matching, or serializable result values.
Choose onrails when
- You want a tagged-union data model you can serialize and pattern-match.
- You want
Maybeand exhaustivematchfrom the same toolkit. - You like having both data-first/curried functions and an optional fluent wrapper, rather than one fixed style.