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 { 
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 failure: Result.Failure<"error">
failure
=
import Result
Result
.
const assertFailure: <Result.Result<never, "error">>(result: Result.Result<never, "error">) => Result.Failure<"error"> (+1 overload)
assertFailure
(
const result: Result.Result<never, "error">
result
);
// failure: { type: 'Failure', error: 'error' }
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);
import Result
Result
.
const assertFailure: <Result.ResultAsync<never, any>>(result: Result.ResultAsync<never, any>) => Promise<Result.Failure<any>> (+1 overload)
assertFailure
(result); // throws Error
No overload matches this call. Overload 1 of 2, '(result: ResultAsync<never, any>): Promise<Failure<any>>', gave the following error. Argument of type 'Result<42, never>' is not assignable to parameter of type 'ResultAsync<never, any>'. Type 'Success<42>' 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<42, never>' is not assignable to parameter of type 'Result<never, any>'. Type 'Success<42>' is not assignable to type 'Result<never, any>'. Type 'Success<42>' is not assignable to type 'Success<never>'. Type '42' 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 fail: <"error">(error: "error") => Result.Result<never, "error"> (+1 overload)
fail
('error');
const
const value: any
value
=
import Result
Result
.
const pipe: <Result.Result<number, string>, Result.Result<never, string>, Result.Failure<any>, any>(a: Result.Result<number, string>, ab: (a: Result.Result<number, string>) => Result.Result<never, string>, bc: (b: Result.Result<never, string>) => Result.Failure<any>, cd: (c: Result.Failure<any>) => any) => any (+25 overloads)
pipe
(
const getResult: () => Result.Result<number, string>
getResult
(),
import Result
Result
.
const andThen: <Result.Result<number, string>, Result.Result<never, "die">>(fn: (a: number) => Result.Result<never, "die">) => (result: Result.Result<number, string>) => Result.Result<never, string> (+1 overload)
andThen
(() =>
import Result
Result
.
const fail: <"die">(error: "die") => Result.Result<never, "die"> (+1 overload)
fail
('die')),
import Result
Result
.
const assertFailure: {
    <R extends Result.ResultAsync<never, any>>(result: R): Promise<Result.Failure<Result.InferFailure<R>>>;
    <R extends Result.Result<never, any>>(result: R): Result.Failure<Result.InferFailure<R>>;
}

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

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

const result = Result.fail('error');
const failure = Result.assertFailure(result);
// failure: { type: 'Failure', error: 'error' }
@example

Throws on Success

//
@errors

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

const result = Result.succeed(42); Result.assertFailure(result); // throws Error

@example

Safe unwrap with assertFailure

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
);
@seeunwrapError - Use with assertFailure to safely unwrap error values.@categoryAssertions
assertFailure
,
import Result
Result
.
const unwrapError: <Result.Failure<any>>() => (result: Result.Failure<any>) => any (+3 overloads)
unwrapError
(), // Safe unwrap after assertion
);

See

unwrapError - Use with assertFailure to safely unwrap error values.

Call Signature

assertFailure<R>(result): Promise<Failure<InferFailure<R>>>

Type Parameters

R

R extends ResultAsync<never, any>

Parameters

result

R

Returns

Promise<Failure<InferFailure<R>>>

Call Signature

assertFailure<R>(result): Failure<InferFailure<R>>

Type Parameters

R

R extends Result<never, any>

Parameters

result

R

Returns

Failure<InferFailure<R>>