unwrap
Extract the value from an Ok result, or throw on Err. Mirrors Rust's Result::unwrap / Result::expect.
Usage
unwrap(result, message?)
unwrap is the escape hatch back to throwing — useful at module boundaries where you can't propagate a Result further (program startup, top-level scripts, test setup). Prefer pattern matching or isOk/isErr guards inside normal code.
With no message, the original Error from Err is thrown directly. With a message, a new Error is thrown with the original preserved on cause — same shape as Rust's expect.
Parameters
resultResult<T>required
the result to unwrap.
messagestringoptional
a custom error message. Original error becomes cause.
Returns
the unwrapped value.
Throws
- The original
ErrorfromErr, if nomessageis provided. - A new
Error(message, { cause: original }), ifmessageis provided.
See also
isOk/isErr— non-throwing alternatives- Result & Errors concept guide