onrails logoonrails

Getting Started

How to install and start using onrails in your project.

Install

Add the three primitives — Result, Maybe, and exhaustive match. They're independent; install only what you need.

bun add @onrails/result @onrails/maybe @onrails/pattern

Return a Result instead of throwing

A fallible function returns a Result<T, E> — ok on the happy path, a typed err on the expected failure. The core is data-first: import the package as a namespace (R) and pass the result as the first argument. map transforms the Ok channel; match collapses both branches into one value — no thrown exceptions on the expected path.

import * as  from "@onrails/result";

// Fallible calculation — Ok or a typed Err, never a throw.
function (: string): .<number, string> {
  const  = .(, 10);
  return .() ? .("Not a number") : .();
}

const  = .(
  .(("42"), () =>  * 2),
  () => `Success: ${}`,
  () => `Error: ${}`,
);

.(outcome);
const outcome: string

Data-first, not fluent

The sync core is data-first: R.map(result, fn), not result.map(fn). Reach for pipe / flow to chain steps point-free. ResultAsync is fluent (.flatMap, .match) — see below.

Real I/O: one typed error channel

ResultAsync wraps async work and chains fluently. tryAsync turns a throwing promise into a typed channel; match settles it once and forces you to handle every variant. This snippet is extracted from a real, type-checked module in @onrails/examples — it can't drift from code that compiles.

import { ,  } from "@onrails/result";
type  =
  | { : "network"; : unknown }
  | { : "not_found" }
  | { : "decode"; : string[] };

// neverthrow-style: replace try/catch around fetch with one typed error channel.
const  = (: string): <, > =>
  (.(), ():  => ({ : "network",  }))
    .(() =>
       === null ? .({ : "not_found" as  }) : .(),
    )
    .();

// Settle once and handle every error in the union — exhaustively, no throws.
const  = (: string) =>
  ().(
    () => ({ : 200, :  }),
    () =>
      . === "not_found"
        ? { : 404, : "No such user" }
        : { : 502, : "Upstream unavailable" },
  );

Keep Result at boundaries: don't return Promise<Result<…>> from public signatures — return ResultAsync<T, E> instead. @onrails/eslint-plugin and @onrails/biome-plugin enforce this for consumers.

Next: Recipes for composition patterns, Testing to see why these primitives test cleanly, or the API reference for Result, Maybe, and match.

On this page