#Function: assertSuccess()
assertSuccess<
R>(result):trueextendsHasPromise<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 .function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (42);
const const success: Result.Success<42> success = import Result Result .function 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.
@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.@exampleimport { Result } from '@praha/byethrow';
const result = Result.succeed(42);
const success = Result.assertSuccess(result);
// success: { type: 'Success', value: 42 }
@exampleType-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 }Type-safe usage after narrowing error type
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.@exampleimport { 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 .function succeed<42>(value: 42): Result.Result<42, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (42);
const const value: number value = import Result Result .function 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)Applies a sequence of functions to a value, from left to right.
This utility enables function composition in a left-to-right manner.
It supports up to 26 chained functions from A through Z.
@typeParamA - The initial input value type.@typeParamB-Z - Intermediate and final function return types.@paramvalue - The initial input value.@paramfunctions - Additional unary functions composing of the previous result.@returnsThe final result after applying all functions.@exampleimport { Result } from '@praha/byethrow';
const result = Result.pipe(
2,
x => x + 1,
x => x * 3,
x => `Result: ${x}`
);
// Result: 9
@categoryUtilities pipe (
const getResult: () => Result.Result<number, string> getResult (),
import Result Result .function 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)Chains the next computation using the error value of a
Result
or
ResultAsync
.
If the original result is a
Success
, it is returned unchanged.
Otherwise, the provided function is called, and its result is returned as-is.
@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@exampleSuccess Case
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.succeed(42),
Result.orElse((error) => Result.succeed(0)),
);
// { type: 'Success', value: 42 }
@exampleFailure Case (function returns a Success)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('original error'),
Result.orElse((error) => Result.succeed('default value')),
);
// result: { type: 'Success', value: 'default value' }
@exampleFailure Case (function returns a Failure)
import { Result } from '@praha/byethrow';
const result = Result.pipe(
Result.fail('original error'),
Result.orElse((error) => Result.fail('new error: ' + error)),
);
// result: { type: 'Failure', error: 'new error: original error' }
@seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators orElse (() => import Result Result .function succeed<0>(value: 0): Result.Result<0, never> (+1 overload)Creates a
Success
result from a given value.
Passing a Promise is allowed, but it is recommended to await it before passing to succeed.
@typeParamT - The type of the value to wrap.@returnsA Result containing the given value.@exampleSynchronous Usage
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
// Result.Result<42, never>
@exampleWith No Value
import { Result } from '@praha/byethrow';
const result = Result.succeed();
// Result.Result<void, never>
@seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators succeed (0)), // Error type becomes never
import Result Result .function 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.
@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.@exampleimport { Result } from '@praha/byethrow';
const result = Result.succeed(42);
const success = Result.assertSuccess(result);
// success: { type: 'Success', value: 42 }
@exampleType-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 .function unwrap<Result.Success<number>>(): (result: Result.Success<number>) => number (+3 overloads)Extracts the success value from a
Result
or
ResultAsync
.
If the input is a
Failure
, it will throw the error or return the default value if provided.
@typeParamR - The input Result or ResultAsync .@typeParamT - The default value type (optional).@exampleSuccess Case (without default)
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
const value = Result.unwrap(result); // 42
@exampleSuccess Case (with default)
import { Result } from '@praha/byethrow';
const result = Result.succeed(42);
const value = Result.unwrap(result, 0); // 42
@exampleFailure Case (without default) - throws error
import { Result } from '@praha/byethrow';
const result = Result.fail('error');
Result.unwrap(result); // throws 'error'
@exampleFailure Case (with default)
import { Result } from '@praha/byethrow';
const result = Result.fail('error');
const value = Result.unwrap(result, 0); // 0
@seeassertSuccess - When used with assertSuccess , you can safely unwrap the Result .@categoryUnwraps unwrap (), // Safe unwrap after assertion
);#See
unwrap - Use with assertSuccess to safely unwrap success values.
