Function: unwrap()

Extracts the success value from a Result or ResultAsync.

If the input is a Failure, it will throw the error or return the default value if provided.

Type Param

The input Result or ResultAsync.

Type Param

The default value type (optional).

Examples

import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<42, never>
result
=
import Result
Result
.
const succeed: <42>(value: 42) => Result.Result<42, never> (+1 overload)
succeed
(42);
const
const value: 42
value
=
import Result
Result
.
const unwrap: <Result.Result<42, never>>(result: Result.Result<42, never>) => 42 (+3 overloads)
unwrap
(
const result: Result.Result<42, never>
result
); // 42
import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<42, never>
result
=
import Result
Result
.
const succeed: <42>(value: 42) => Result.Result<42, never> (+1 overload)
succeed
(42);
const
const value: 42 | 0
value
=
import Result
Result
.
const unwrap: <Result.Result<42, never>, 0>(result: Result.Result<42, never>, defaultValue: 0) => 42 | 0 (+3 overloads)
unwrap
(
const result: Result.Result<42, never>
result
, 0); // 42
import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<never, "error">
result
=
import Result
Result
.
const fail: <"error">(error: "error") => Result.Result<never, "error"> (+1 overload)
fail
('error');
import Result
Result
.
const unwrap: <Result.Result<never, "error">>(result: Result.Result<never, "error">) => never (+3 overloads)
unwrap
(
const result: Result.Result<never, "error">
result
); // throws 'error'
import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<never, "error">
result
=
import Result
Result
.
const fail: <"error">(error: "error") => Result.Result<never, "error"> (+1 overload)
fail
('error');
const
const value: 0
value
=
import Result
Result
.
const unwrap: <Result.Result<never, "error">, 0>(result: Result.Result<never, "error">, defaultValue: 0) => 0 (+3 overloads)
unwrap
(
const result: Result.Result<never, "error">
result
, 0); // 0

See

assertSuccess - When used with assertSuccess, you can safely unwrap the Result.

Call Signature

unwrap<R>(result): true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Type Parameters

R

R extends ResultMaybeAsync<any, any>

Parameters

result

R

Returns

true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Call Signature

unwrap<R, T>(result, defaultValue): true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>

Type Parameters

R

R extends ResultMaybeAsync<any, any>

T

T

Parameters

result

R

defaultValue

T

Returns

true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>

Call Signature

unwrap<R>(): (result) => true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Type Parameters

R

R extends ResultMaybeAsync<any, any>

Returns

(result): true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Parameters

result

R

Returns

true extends HasPromise<R> ? Promise<InferSuccess<R>> : InferSuccess<R>

Call Signature

unwrap<R, T>(defaultValue): (result) => true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>

Type Parameters

R

R extends ResultMaybeAsync<any, any>

T

T

Parameters

defaultValue

T

Returns

(result): true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>

Parameters

result

R

Returns

true extends HasPromise<R> ? Promise<T | InferSuccess<R>> : T | InferSuccess<R>