Function: assertSuccess()

assertSuccess<R>(result): true extends HasPromise<R> ? Promise<Success<InferSuccess<R>>> : Success<InferSuccess<R>>

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

Type Parameters

R

R extends ResultMaybeAsync<any, never>

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

Parameters

result

R

The Result or ResultAsync to assert as a Success. The error type must be never.

Returns

true extends HasPromise<R> ? Promise<Success<InferSuccess<R>>> : Success<InferSuccess<R>>

The Success result or a Promise of Success result.

Throws

If the result is a Failure at runtime.

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>

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

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

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

Type-safe usage after narrowing error type

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

const getResult = (): Result.Result<number, string> => Result.succeed(42);
const value = Result.pipe(
getResult(),
Result.orElse(() => Result.succeed(0)), // Error type becomes never
Result.assertSuccess, // Type-safe: error type is now never
Result.unwrap(), // Safe unwrap after assertion
);
@seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
assertSuccess
(
const result: Result.Result<42, never>
result
);
// success: { type: 'Success', value: 42 }
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: number
value
=
import Result
Result
.
const pipe: <Result.Result<number, string>, Result.Result<number, never>, Result.Success<number>, number>(a: Result.Result<number, string>, ab: (a: Result.Result<number, string>) => Result.Result<number, never>, bc: (b: Result.Result<number, never>) => Result.Success<number>, cd: (c: Result.Success<number>) => number) => number (+25 overloads)
pipe
(
const getResult: () => Result.Result<number, string>
getResult
(),
import Result
Result
.
const orElse: <Result.Result<number, string>, Result.Result<0, never>>(fn: (a: string) => Result.Result<0, never>) => (result: Result.Result<number, string>) => Result.Result<number, never> (+1 overload)
orElse
(() =>
import Result
Result
.
const succeed: <0>(value: 0) => Result.Result<0, never> (+1 overload)
succeed
(0)), // Error type becomes never
import Result
Result
.
const assertSuccess: <R extends Result.ResultMaybeAsync<any, never>>(result: R) => true extends HasPromise<R> ? Promise<Result.Success<Result.InferSuccess<R>>> : Result.Success<Result.InferSuccess<R>>

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

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

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

Type-safe usage after narrowing error type

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

const getResult = (): Result.Result<number, string> => Result.succeed(42);
const value = Result.pipe(
getResult(),
Result.orElse(() => Result.succeed(0)), // Error type becomes never
Result.assertSuccess, // Type-safe: error type is now never
Result.unwrap(), // Safe unwrap after assertion
);
@seeunwrap - Use with assertSuccess to safely unwrap success values.@categoryAssertions
assertSuccess
, // Type-safe: error type is now never
import Result
Result
.
const unwrap: <Result.Success<number>>() => (result: Result.Success<number>) => number (+3 overloads)
unwrap
(), // Safe unwrap after assertion
);

See

unwrap - Use with assertSuccess to safely unwrap success values.