logo
byethrow
Guide
Examples
API Reference
Guide
Examples
API Reference
logo
byethrow
@praha/byethrow

Modules

Result

Types

Type Alias: Failure<E>
Type Alias: InferFailure<T>
Type Alias: InferSuccess<T>
Type Alias: Result<T, E>
Type Alias: ResultAsync<T, E>
Type Alias: ResultFor<R, T, E>
Type Alias: ResultMaybeAsync<T, E>
Type Alias: Success<T>

Functions

Function: andThen()
Function: andThrough()
Function: assertFailure()
Function: assertSuccess()
Function: bind()
Function: combine()
Function: do()
Function: fail()
Function: inspect()
Function: inspectError()
Function: isFailure()
Function: isResult()
Function: isSuccess()
Function: map()
Function: mapError()
Function: orElse()
Function: parse()
Function: pipe()
Function: succeed()
Function: try()
Function: unwrap()
Function: unwrapError()

Last Updated:

Previous PageFunction: assertFailure()
Next PageFunction: bind()

#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<number, never>
result
=
import Result
Result
.
const succeed: <number>(value: number) => Result.Result<number, never> (+1 overload)
succeed
(42);
const
const success: Result.Success<number>
success
=
import Result
Result
.
const assertSuccess: <Result.Result<number, never>>(result: Result.Result<number, never>) => Result.Success<number> (+1 overload)
assertSuccess
(
const result: Result.Result<number, never>
result
);
// success: { type: 'Success', value: 42 }
import { 
import Result
Result
} from '@praha/byethrow';
const
const result: Result.Result<never, string>
result
=
import Result
Result
.
const fail: <string>(error: string) => Result.Result<never, string> (+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, 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 { 
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: <number>(value: number) => Result.Result<number, never> (+1 overload)
succeed
(42);
const
const value: any
value
=
import Result
Result
.
const pipe: <Result.Result<number, string>, Result.Result<string | number, never>, Result.Success<any>, any>(a: Result.Result<number, string>, ab: (a: Result.Result<number, string>) => Result.Result<string | number, never>, bc: (b: Result.Result<string | number, 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<string, never>>(fn: (a: string) => Result.Result<string, never>) => (result: Result.Result<number, string>) => Result.Result<string | number, never> (+1 overload)
orElse
(() =>
import Result
Result
.
const succeed: <string>(value: string) => Result.Result<string, 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>>>

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