Function: assertFailure()

assertFailure<R>(result): true extends HasPromise<R> ? Promise<Failure<InferFailure<R>>> : Failure<InferFailure<R>>

Asserts that a Result or ResultAsync is a Failure and returns it. This function requires that the result's success type is never, meaning the result is guaranteed to be a Failure at the type level. If the result is a Success at runtime, throws an error.

Type Parameters

R

R extends ResultMaybeAsync<never, any>

The result type that extends ResultMaybeAsync with never as the success type.

Parameters

result

R

The Result or ResultAsync to assert as a Failure. The success type must be never.

Returns

true extends HasPromise<R> ? Promise<Failure<InferFailure<R>>> : Failure<InferFailure<R>>

The Failure result or a Promise of Failure result.

Throws

If the result is a Success at runtime.

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">

Asserts that a Result or ResultAsync is a Failure and returns it. This function requires that the result's success type is never, meaning the result is guaranteed to be a Failure at the type level. If the result is a Success at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the success type.@paramresult - The Result or ResultAsync to assert as a Failure. The success type must be never.@returnsThe Failure result or a Promise of Failure result.@throws{Error} If the result is a Success at runtime.@example
import { Result } from '@praha/byethrow';

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

Type-safe usage after narrowing success type

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

const getResult = (): Result.Result<number, string> => Result.fail('error');
const value = Result.pipe(
getResult(),
Result.andThen(() => Result.fail('die')), // Success type becomes never
Result.assertFailure, // Type-safe: success type is now never
Result.unwrapError(), // Safe unwrap after assertion
);
@seeunwrapError - Use with assertFailure to safely unwrap error values.@categoryAssertions
assertFailure
(
const result: Result.Result<never, "error">
result
);
// failure: { type: 'Failure', error: 'error' }
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: string
value
=
import Result
Result
.
const pipe: <Result.Result<number, string>, Result.Result<never, string>, Result.Failure<string>, string>(a: Result.Result<number, string>, ab: (a: Result.Result<number, string>) => Result.Result<never, string>, bc: (b: Result.Result<never, string>) => Result.Failure<string>, cd: (c: Result.Failure<string>) => string) => string (+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')), // Success type becomes never
import Result
Result
.
const assertFailure: <R extends Result.ResultMaybeAsync<never, any>>(result: R) => true extends HasPromise<R> ? Promise<Result.Failure<Result.InferFailure<R>>> : Result.Failure<Result.InferFailure<R>>

Asserts that a Result or ResultAsync is a Failure and returns it. This function requires that the result's success type is never, meaning the result is guaranteed to be a Failure at the type level. If the result is a Success at runtime, throws an error.

@function@typeParamR - The result type that extends ResultMaybeAsync with never as the success type.@paramresult - The Result or ResultAsync to assert as a Failure. The success type must be never.@returnsThe Failure result or a Promise of Failure result.@throws{Error} If the result is a Success at runtime.@example
import { Result } from '@praha/byethrow';

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

Type-safe usage after narrowing success type

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

const getResult = (): Result.Result<number, string> => Result.fail('error');
const value = Result.pipe(
getResult(),
Result.andThen(() => Result.fail('die')), // Success type becomes never
Result.assertFailure, // Type-safe: success type is now never
Result.unwrapError(), // Safe unwrap after assertion
);
@seeunwrapError - Use with assertFailure to safely unwrap error values.@categoryAssertions
assertFailure
, // Type-safe: success type is now never
import Result
Result
.
const unwrapError: <Result.Failure<string>>() => (result: Result.Failure<string>) => string (+3 overloads)
unwrapError
(), // Safe unwrap after assertion
);

See

unwrapError - Use with assertFailure to safely unwrap error values.