onrails logoonrails

Core concepts

The railway model, the three primitives, and the one hard constraint — serializable data.

onrails is three small primitives and one discipline. This page is the mental model; the API reference is the surface.

The railway

A fallible step has two tracks: the Ok track carries the value forward, the Err track carries a typed failure straight to the end. Composition keeps you on the Ok track and short-circuits to Err on the first failure — so the happy path reads top-to-bottom with no try/catch and no null checks interleaved.

import * as  from "@onrails/result";

// Each step either advances the Ok value or diverts to Err — once on Err, the
// rest is skipped and the error falls through to the end.
const  = .(
  .(7),
  .(() =>  * 2),
  .(() => ( < 100 ? .() : .("too big"))),
); // Result<number, "too big">

result;
const result: R.Result<number, "too big">

Three primitives, three jobs

PrimitiveModelsReach for it when
Result<T, E> / ResultAsync<T, E>expected failurethe caller needs to inspect why it failed
Maybe<T>expected absencea value is normally missing, and that's fine
match (@onrails/pattern)exhaustive branchingcollapsing a tagged union, with no case forgotten

The distinction is the whole design:

  • Expected failure — a parser on malformed input. The caller wants the diagnostic, so it goes in a Result's Err channel.
  • Expected absence — a cache miss, an optional relation, no match. Nothing went wrong, so it's a Maybe, not an error.
  • Defect — a third-party throw (e.g. inside JSON.parse). It's not part of ordinary flow until you convert it at an explicit boundary with trySync / tryAsync.
import { , ,  } from "@onrails/maybe";

// A cache miss is expected absence — Maybe, not Result.
const  = new <string, string>();
const  = (
  ((.("u1")), () => `Hi ${}`),
  () => ,
  () => "Hello, stranger",
);

greeting;
const greeting: string

Data-first core, fluent async

The sync core is data-first — every transform takes the value as its first argument: map(result, fn), not result.map(fn). Each transform is also dual-form: call it curried (map(fn)) to drop straight into pipe / flow.

import { , , , ,  } from "@onrails/result";

// curried form composes point-free — value supplied later
const  = (
  ((: number) =>  + 1),
  (() => ( > 0 ? () : ("non-positive"))),
);

const checked = ((41));
const checked: Result<number, "non-positive">

ResultAsync<T, E> is fluent — .map, .flatMap, .match chain directly — because awaiting a half-built pipeline reads better than nesting. (For neverthrow muscle memory, .andThen is an alias of .flatMap.)

House style: import * as R

The namespace import is the documented house style, not a stylistic option:

import * as  from "@onrails/result";
import * as  from "@onrails/maybe";
import * as  from "@onrails/pattern";

.(.(1), () =>  + 1);
.(.(1), () =>  + 1);

Two reasons: R. autocompletes the whole module surface — the one ergonomic debit data-first has versus methods — and a namespace import sidesteps name collisions, most commonly match from ts-pattern or @onrails/pattern itself (R.match next to P.matchTag, no rename gymnastics). Named imports (import { map } from "@onrails/result") still work and read fine in small files; reach for the namespace import once a file pulls in more than a handful of names, or mixes carriers.

The fluent bracket: open, chain, close

/fluent (@onrails/result/fluent, @onrails/maybe/fluent) wraps data in closures so a chain can read as .map().flatMap().match() instead of nested calls. It's sugar for the app edge — never for library or service internals — and it comes with one rule: the wrapper stays local.

// opens with fluent(...), closes with a terminal in the same expression
const  = (('{"v":1}'))
  .(() => . + 1)
  .(
    () => `ok: ${}`,
    () => `err: ${}`,
  );

Think of fluent(...) and a terminal (.toResult(), .toMaybe(), .toString(), .match(), .unwrapOr()) as a matched bracket pair — the wrapper must open and close within one expression. It's a closure over plain data, not plain data itself, so it can never be returned, exported, stored on a field, or handed to postMessage() / structuredClone() / JSON.stringify() — none of that would serialize correctly. @onrails/eslint-plugin and @onrails/biome-plugin both ship a fluent-stays-local rule that flags exactly those four escape points.

The one hard constraint: serializable data

A Result is a tagged union — { _tag: "Ok"; value } or { _tag: "Err"; error } — whose own enumerable fields are plain data. The methods (map, flatMap, match, …) live on a shared non-enumerable prototype, so JSON.stringify output is exactly the tagged shape. The same for Maybe. That's deliberate and non-negotiable: the data crosses process and network boundaries (RSC payloads, workers, queues) and survives JSON. After JSON.parse, structuredClone or postMessage the prototype is gone, so lift the data back with Result.from / Maybe.from.

Why it matters

Ergonomics are tuned freely, but serializability is the line we don't cross. A Result you can't send through a worker postMessage or an RSC boundary would defeat the point. Tagged unions in, classes out.

Next: Recipes for the composition tiers, or Testing to see how inert data pays off under test.

On this page