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 { 
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 success: Result.Success<42>
success
=
import Result
Result
.
const assertSuccess: <Result.Result<42, never>>(result: Result.Result<42, never>) => Result.Success<42> (+1 overload)
assertSuccess
(
const result: Result.Result<42, never>
result
);
// success: { type: 'Success', value: 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 assertSuccess: <Result.ResultAsync<any, never>>(result: Result.ResultAsync<any, never>) => Promise<Result.Success<any>> (+1 overload)
assertSuccess
(result); // throws Error
No overload matches this call. Overload 1 of 2, '(result: ResultAsync<any, never>): Promise<Success<any>>', gave the following error. Argument of type 'Result<never, "error">' 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, "error">' is not assignable to parameter of type 'Result<any, never>'. Type 'Failure<"error">' is not assignable to type 'Result<any, never>'. Type 'Failure<"error">' is not assignable to type 'Failure<never>'. Type '"error"' is not assignable to type 'never'.
import { 
import Result
Result
} from '@praha/byethrow';
const
const getResult: () => Result.Result<number, string>
getResult
= ():
import Result
Result
.
type Result<T, E> = Result.Success<T> | Result.Failure<E>

A union type representing either a success or a failure.

@typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
import { Result } from '@praha/byethrow';

const doSomething = (): Result.Result<number, string> => {
  return Math.random() > 0.5
    ? { type: 'Success', value: 10 }
    : { type: 'Failure', error: 'Oops' };
};
@categoryCore Types
Result
<number, string> =>
import Result
Result
.
const succeed: <42>(value: 42) => Result.Result<42, never> (+1 overload)
succeed
(42);
const
const value: any
value
=
import Result
Result
.
const pipe: <Result.Result<number, string>, Result.Result<number | "fallback", never>, Result.Success<any>, any>(a: Result.Result<number, string>, ab: (a: Result.Result<number, string>) => Result.Result<number | "fallback", never>, bc: (b: Result.Result<number | "fallback", never>) => Result.Success<any>, cd: (c: Result.Success<any>) => any) => any (+25 overloads)
pipe
(
const getResult: () => Result.Result<number, string>
getResult
(),
import Result
Result
.
const orElse: <Result.Result<number, string>, Result.Result<"fallback", never>>(fn: (a: string) => Result.Result<"fallback", never>) => (result: Result.Result<number, string>) => Result.Result<number | "fallback", never> (+1 overload)
orElse
(() =>
import Result
Result
.
const succeed: <"fallback">(value: "fallback") => Result.Result<"fallback", never> (+1 overload)
succeed
('fallback')),
import Result
Result
.
const assertSuccess: {
    <R extends Result.ResultAsync<any, never>>(result: R): Promise<Result.Success<Result.InferSuccess<R>>>;
    <R extends Result.Result<any, never>>(result: R): Result.Success<Result.InferSuccess<R>>;
}

Asserts that a Result or ResultAsync is a Success and returns it. If the result is a Failure , throws an error.

@function@typeParamT - The type of the success value.@paramresult - The Result or ResultAsync to assert as a Success.@returnsThe Success result or a Promise of Success result.@throws{Error} If the result is a Failure .@example
import { Result } from '@praha/byethrow';

const result = Result.succeed(42);
const success = Result.assertSuccess(result);
// success: { type: 'Success', value: 42 }
@example

Throws on Failure

//
@errors

: 2769 import { Result } from '@praha/byethrow';

const result = Result.fail('error'); Result.assertSuccess(result); // throws Error

@example

Safe unwrap with assertSuccess

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
);
@seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
assertSuccess
,
import Result
Result
.
const unwrap: <Result.Success<any>>() => (result: Result.Success<any>) => any (+3 overloads)
unwrap
(), // Safe unwrap after assertion
);

See

unwrap - Use with assertSuccess to safely unwrap success values.

Call Signature

assertSuccess<R>(result): Promise<Success<InferSuccess<R>>>

Type Parameters

R

R extends ResultAsync<any, never>

Parameters

result

R

Returns

Promise<Success<InferSuccess<R>>>

Call Signature

assertSuccess<R>(result): Success<InferSuccess<R>>

Type Parameters

R

R extends Result<any, never>

Parameters

result

R

Returns

Success<InferSuccess<R>>