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