> For AI agents: the complete documentation index is available at https://massaman.dev/llms.txt, the full documentation bundle is available at https://massaman.dev/llms-full.txt.

# unwrap

Extract the value from an `Ok` result, or throw on `Err`. Mirrors Rust's `Result::unwrap` / `Result::expect`.

```typescript
const value = unwrap(result)
const value = unwrap(result, 'config required')
```

## 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.

```typescript
import { attempt, unwrap } from 'massaman'

// Boot-time config — failure is fatal, throwing is fine.
const config = unwrap(
  attempt(() => JSON.parse(readFileSync('./config.json', 'utf8'))),
  'config file is missing or invalid',
)
```

With no `message`, the original typed `Error` from `Err<E>` 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, E>required

the result to unwrap.


messagestringoptional

a custom error message. Original error becomes `cause`.

#### Returns

the unwrapped value.

```typescript
T
```

#### Throws

- The original `E extends Error` from `Err<E>`, if no `message` is provided.
- A new `Error(message, { cause: original })`, if `message` is provided.

## See also

- [`unwrapOr`](https://massaman.dev/reference/control/unwrapOr.md) — return a fallback instead of throwing
- [`isOk`](https://massaman.dev/reference/control/isOk.md) / [`isErr`](https://massaman.dev/reference/control/isErr.md) — non-throwing alternatives
- [Result & Errors concept guide](https://massaman.dev/concepts/result.md)
