UNPKG

@onrails/result

Version:

Tagged Result / ResultAsync for railway-oriented TypeScript — pure tagged unions, neverthrow-shaped compat shim, FL-friendly

50 lines (47 loc) 1.31 kB
import { R as Result } from './types-BQ9vv0nD.js'; /** * Unwraps a {@link Result} inside a {@link tryGen} block — the sync analogue of * Rust's `?` operator. On `Ok` it returns the value; on `Err` it short-circuits * the enclosing `tryGen`, which returns that `Err`. Only valid inside `tryGen`; * prefer {@link flatMap} / `pipe` for long pipelines. * * @returns the unwrapped `Ok` value * * @example * ```ts * const out = tryGen(() => { * const a = yieldResult(parseA()); // unwrap or short-circuit * const b = yieldResult(parseB()); * return ok(a + b); * }); * ``` */ declare const yieldResult: <T, E>(result: Result<T, E>) => T; /** * Terse alias of {@link yieldResult} (mirrors Rust's `?`), for compact * {@link tryGen} blocks. * * @example * ```ts * const out = tryGen(() => { * const a = $(parseA()); * const b = $(parseB()); * return ok(a + b); * }); * ``` */ declare const $: <T, E>(result: Result<T, E>) => T; /** * Run a block that uses {@link yieldResult}; returns the final `Result` or the first Err. * * @example * ```ts * const out = tryGen(() => { * const a = $(parseA()); * const b = $(parseB()); * return ok(a + b); * }); * ``` */ declare const tryGen: <T, E>(fn: () => Result<T, E>) => Result<T, E>; export { $, tryGen, yieldResult };