Function: assertFailure()
Asserts that a Result or ResultAsync is a Failure and returns it.
If the result is a Success, throws an error.
Type Param
The type of the error value.
Param
The Result or ResultAsync to assert as a Failure.
Throws
If the result is a Success.
Examples
import { Result } from '@praha/byethrow';
const result = Result.fail('error');
const failure = Result.assertFailure(result);
// failure: { type: 'Failure', error: 'error' }
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
Result.assertFailure(result); // throws ErrorNo overload matches this call.
Overload 1 of 2, '(result: ResultAsync<never, any>): Promise<Failure<any>>', gave the following error.
Argument of type 'Result<number, never>' is not assignable to parameter of type 'ResultAsync<never, any>'.
Type 'Failure<never>' is missing the following properties from type 'Promise<Result<never, any>>': then, catch, finally, [Symbol.toStringTag]
Overload 2 of 2, '(result: Result<never, any>): Failure<any>', gave the following error.
Argument of type 'Result<number, never>' is not assignable to parameter of type 'Result<never, any>'.
Type 'Success<number>' is not assignable to type 'Result<never, any>'.
Type 'Success<number>' is not assignable to type 'Success<never>'.
Type 'number' is not assignable to type 'never'.
import { Result } from '@praha/byethrow';
const getResult = (): Result.Result<number, string> => Result.fail('error');
const value = Result.pipe(
getResult(),
Result.andThen(() => Result.fail('die')),
Result.assertFailure,
Result.unwrapError(), // Safe unwrap after assertion
);
See
unwrapError - Use with assertFailure
to safely unwrap error values.
Call Signature
assertFailure<R
>(result
): Promise
<Failure
<InferFailure
<R
>>>
Defined in: functions/assert-failure.ts:54
Type Parameters
R
R
extends ResultAsync
<never
, any
>
Parameters
result
R
Returns
Promise
<Failure
<InferFailure
<R
>>>
Call Signature
assertFailure<R
>(result
): Failure
<InferFailure
<R
>>
Defined in: functions/assert-failure.ts:55
Type Parameters
R
R
extends Result
<never
, any
>
Parameters
result
R
Returns
Failure
<InferFailure
<R
>>