blob: 84e658eedb0ea80a82f71cbd72d4b6d593ecf89d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import type { Result } from './types'
/**
* Extract the value from an ok result; throw on error. Reserve for tests
* and assertion contexts — in production code prefer branching on isOk.
*/
export function unwrap<T, E>(r: Result<T, E>): T {
if (!r.ok) {
throw new Error(`unwrap() called on error result: ${String(r.error)}`)
}
return r.value
}
/** Extract the value or return a fallback. */
export function unwrapOr<T, E>(r: Result<T, E>, fallback: T): T {
return r.ok ? r.value : fallback
}
|