Skip to content

unwrap

Extracts the value from a successful ParseResult<T>. If the result represents a failure, unwrap throws an Error containing the issue messages. This helper is intentionally explicit: it is the only result helper that may throw.

Use unwrap when you want a clear, fail-fast extraction of normalized or validated values.

Signature

function unwrap<T>(result: ParseResult<T>): T

Parameters

Returns

  • The contained value when result.ok === true.
  • An Error when result.ok === false.

  • The error message is derived from result.issues:

  • A single issue becomes the message.
  • Multiple issues are joined with "; ".
  • An empty issue list produces "Unwrap failed: no value present".

Behavior

  • Never mutates input.
  • Returns an error only when the ok is false.
  • Produces deterministic, human-readable error messages.
  • Safe to use after a parser or validator when failure is impossible or should be treated as exceptional.

Examples

unwrap(ok(123))
// 123

unwrap(error("Invalid input"))
// throws Error("Invalid input")

unwrap(error(["Missing field", "Invalid format"]))
// throws Error("Missing field; Invalid format")