Skip to content

unwrapOr

Safely extracts the value from a ParseResult<T>. If the result represents success, the contained value is returned. If the result represents failure, the provided fallback value is returned instead of throwing.

unwrapOr is the non‑throwing counterpart to unwrap, making it ideal for predictable, failure‑tolerant workflows.

Signature

function unwrapOr<T>(result: ParseResult<T>, fallback: T): T

Parameters

Returns

  • The contained value when result.ok === true.
  • The fallback value when result.ok === false.

Behavior

  • Never throws and never mutates input.
  • Returns the fallback exactly as provided.
  • Safe to use in pipelines where failure is expected or non‑exceptional.

Examples

unwrapOr(ok(123), 0)
// 123

unwrapOr(error("Invalid input"), 0)
// 0

unwrapOr(error("Missing"), { a: 1 })
// { a: 1 }