Skip to main content

Function: unwrapOr()

unwrapOr<T, E>(result, defaultValue): T

Defined in: src/types/result.ts:222

Unwraps a Result, returning the value or a default.

Type Parameters

T

Success value type

E extends Error

Error type

Parameters

result

Result<T, E>

Result to unwrap

defaultValue

T

Default value to return if Result is Err

Returns

T

The success value or the default value

Remarks

Use this function when you want to provide a fallback value for errors without exception handling.

Example

const result = Err(new Error('Failed'));
const value = unwrapOr(result, 42); // 42

const okResult = Ok(10);
const value2 = unwrapOr(okResult, 42); // 10