onrails
A lightweight railway-oriented primitive set for TypeScript. Model fallible boundaries, map expected failures, and structure your workflows without exceptions.
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" },
);import { } from "@onrails/pattern";
type =
| { : "loading" }
| { : "empty" }
| { : "ok"; : [] }
| { : "error"; : number };
// ts-pattern-style: render a union to markup, exhaustively.
// Omit a case (or add a variant) and it stops compiling.
const = (: ) =>
()
.({ : "loading" }, () => "<spinner />")
.({ : "empty" }, () => "<p>No posts yet</p>")
.({ : "ok" }, () => `<ul>${..} posts</ul>`)
.({ : "error" }, () => `<p>Failed (${.})</p>`)
.();import { , , , } from "@onrails/maybe";
// fp-ts Option-style: walk nullable fields safely, then supply a default.
// No optional-chaining soup, no scattered null checks.
const = ((.), () => (.));
const = (
(, () => ..()),
"UNKNOWN",
);import { } from "@onrails/maybe";
import { } from "@onrails/maybe/interop";
import { } from "@onrails/result";
import { } from "@onrails/result/railway";
type =
| { : "no_user"; : string }
| { : "empty_cart" }
| { : "declined"; : string };
// Railway threads a growing context object through each step; errors accumulate
// into one union, and every step sees the keys added before it.
const = (: string) =>
.("user", () =>
(
(.()),
(): => ({ : "no_user", : }),
),
)
.("cart", ({ }) => (.))
.("receipt", ({ }) =>
.. === 0
? <{ : string }, >({ : "empty_cart" })
: (),
)
.("orderId", ({ }) => .)
.(({ , }) => ({ , : . }));import { } from "@onrails/maybe";
import { } from "@onrails/maybe/interop";
import { } from "@onrails/pattern";
import * as from "@onrails/result";
type =
| { : "no_user"; : string }
| { : "empty_cart" }
| { : "declined"; : string };
// do-notation: $ unwraps an Ok or short-circuits. Maybe crosses into Result via toResult.
// Annotate the return: $ short-circuits are thrown, so its error types aren't inferred.
const = (: string): .<{ : string }, > =>
.(() => {
const = .(
(
(.()),
(): => ({ : "no_user", : }),
),
);
const = .((.));
if (.. === 0) return .({ : "empty_cart" as });
const = .(());
return .({ : . });
});
// Result's own match forks Ok/Err; @onrails/pattern handles the error variants.
const = .(
("u1"),
() => `Order ${.} confirmed`,
() =>
()
.({ : "no_user" }, () => "Please sign in")
.({ : "empty_cart" }, () => "Your cart is empty")
.({ : "declined" }, () => `Payment declined: ${.}`)
.(),
);Railway-Oriented Programming
Tame expected failure paths using type-safe Result, ResultAsync, and Maybe patterns without throwing exceptions across API boundaries.
Strict Type Inference
Fully typed returns and errors. Compatible with TS strict options like noUncheckedIndexedAccess to guarantee robustness.
Expressive Railway Flow
Chain operations using functional composition: pipe, flow, or fluent builders. Write clean nested contexts via railway-do-notation.
No Runtime Bloat
Zero external dependencies at runtime. Thin, pure TypeScript wrappers with maximum optimization and native interop.