0Pricing
TypeScript Academy · Lesson

Railway-Oriented Programming

Chain operations that may fail without try/catch.

The Railway Metaphor

Railway-oriented programming pictures a computation as two parallel tracks: a success track and a failure track. Once you switch to failure, you stay there, skipping the rest.

Chaining Result Functions

Each step takes a value and returns a Result. We want to connect steps so that the first failure short-circuits the chain and is carried to the end.

type Result<T, E> = { ok: true; value: T } | { ok: false; error: E };
const ok = <T>(value: T): Result<T, never> => ({ ok: true, value });
const err = <E>(error: E): Result<never, E> => ({ ok: false, error });

All lessons in this course

  1. The Problem with Throwing Errors
  2. Modeling Result Types
  3. Option and Maybe Types
  4. Railway-Oriented Programming
← Back to TypeScript Academy