For AI agents: the complete documentation index is available at /byethrow/llms.txt, the full documentation bundle is available at /byethrow/llms-full.txt, and this page is available as Markdown at /byethrow/guide/tutorial/basics/creating-results.md.
  • English
  • Creating Results

    Now that you know what a Result looks like, let's create them with the library's helper functions.

    Creating a Success with succeed

    succeed creates a Success result:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // With a value const
    const success: Result.Result<42, never>
    success
    =
    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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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);
    // Type: Result.Result<42, never> // Without a value (void) const
    const voidSuccess: Result.Result<void, never>
    voidSuccess
    =
    import Result
    Result
    .
    function succeed(): Result.Result<void, 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    ();
    // Type: Result.Result<void, never>

    The error type is never because this result can never be a failure. A void success is handy for operations that have no meaningful return value, such as saving or deleting something.

    Creating a Failure with fail

    fail creates a Failure result:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // With an error value const
    const failure: Result.Result<never, "NotFound">
    failure
    =
    import Result
    Result
    .
    function fail<"NotFound">(error: "NotFound"): Result.Result<never, "NotFound"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('NotFound');
    // Type: Result.Result<never, 'NotFound'> // Without a value (void) const
    const voidFailure: Result.Result<never, void>
    voidFailure
    =
    import Result
    Result
    .
    function fail(): Result.Result<never, void> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ();
    // Type: Result.Result<never, void>

    Any value can be used as an error. In real applications, we recommend using dedicated error classes, as described in Custom Error.

    Info

    As the examples above show, succeed and fail keep literal types such as 42 and 'NotFound' instead of widening them to number and string. This makes string literal errors like 'NotFound' easy to distinguish from one another. When you want a wider type, annotate the return type of your function, as shown below.

    Returning Results from Functions

    The most common way to use succeed and fail is to return them from your own functions, in place of return and throw:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const parseAge: (input: string) => Result.Failure<"InvalidAge"> | Result.Success<number>
    parseAge
    = (
    input: string
    input
    : string) => {
    const
    const age: number
    age
    =
    var Number: NumberConstructor
    (value?: any) => number

    An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

    Number
    (
    input: string
    input
    );
    if (!
    var Number: NumberConstructor

    An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

    Number
    .
    NumberConstructor.isInteger(number: unknown): boolean

    Returns true if the value passed is an integer, false otherwise.

    @paramnumber A numeric value.
    isInteger
    (
    const age: number
    age
    ) ||
    const age: number
    age
    < 0) {
    return
    import Result
    Result
    .
    function fail<"InvalidAge">(error: "InvalidAge"): Result.Result<never, "InvalidAge"> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    ('InvalidAge');
    } return
    import Result
    Result
    .
    function succeed<number>(value: number): Result.Result<number, 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    (
    const age: number
    age
    );
    }; const
    const result: Result.Failure<"InvalidAge"> | Result.Success<number>
    result
    =
    const parseAge: (input: string) => Result.Failure<"InvalidAge"> | Result.Success<number>
    parseAge
    ('20');
    // Type: Result.Result<number, 'InvalidAge'>

    TypeScript infers both the success type and the error type from the return statements. You can also annotate the return type explicitly, which is a good practice for exported functions:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const parseAge: (input: string) => Result.Result<number, string>
    parseAge
    = (
    input: string
    input
    : string):
    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> => {
    const
    const age: number
    age
    =
    var Number: NumberConstructor
    (value?: any) => number

    An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

    Number
    (
    input: string
    input
    );
    if (!
    var Number: NumberConstructor

    An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers.

    Number
    .
    NumberConstructor.isInteger(number: unknown): boolean

    Returns true if the value passed is an integer, false otherwise.

    @paramnumber A numeric value.
    isInteger
    (
    const age: number
    age
    ) ||
    const age: number
    age
    < 0) {
    return
    import Result
    Result
    .
    function fail<`Invalid age: ${string}`>(error: `Invalid age: ${string}`): Result.Result<never, `Invalid age: ${string}`> (+1 overload)

    Creates a Failure result from a given error.

    Passing a Promise is allowed, but it is recommended to await it before passing to fail.

    @typeParamE - The type of the error to wrap.@returnsA Result containing the given error.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail('Something went wrong');
    // Result.Result<never, string>
    @example

    With No Value

    import { Result } from '@praha/byethrow';
    
    const result = Result.fail();
    // Result.Result<never, void>
    @seecollect - For collect multiple Results into a single Result.@seesequence - For sequence multiple Results into a single Result.@categoryCreators
    fail
    (`Invalid age: ${
    input: string
    input
    }`);
    } return
    import Result
    Result
    .
    function succeed<number>(value: number): Result.Result<number, 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    (
    const age: number
    age
    );
    };
    Info

    succeed and fail do not await a Promise for you. If you have an async value, await it first, or use an async function whose return type is ResultAsync:

    const 
    const getName: () => Result.ResultAsync<string, never>
    getName
    = async ():
    import Result
    Result
    .
    type ResultAsync<T, E> = Promise<Result.Result<T, E>>

    An asynchronous variant of Result , wrapped in a Promise.

    @typeParamT - The type of the Success value.@typeParamE - The type of the Failure value.@example
    import { Result } from '@praha/byethrow';
    
    const fetchData = async (): Result.ResultAsync<string, Error> => {
      try {
        const data = await fetch('...');
        return { type: 'Success', value: await data.text() };
      } catch (err) {
        return { type: 'Failure', error: err as Error };
      }
    };
    @categoryCore Types
    ResultAsync
    <string, never> => {
    const
    const name: string
    name
    = await
    const loadName: () => Promise<string>
    loadName
    ();
    return
    import Result
    Result
    .
    function succeed<string>(value: string): Result.Result<string, 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    (
    const name: string
    name
    );
    };

    Starting a Pipeline with do

    do creates a Success that holds an empty object:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const start: Result.Result<{}, never>
    start
    =
    import Result
    Result
    .
    function do(): Result.Result<{}, never>
    export do

    Alias for succeed({}). Commonly used as a neutral base value in functional chains or monadic pipelines.

    @example
    import { Result } from '@praha/byethrow';
    
    const result = Result.do();
    // Result.Result<{}, never>
    @categoryCreators
    do
    ();
    // Type: Result.Result<{}, never>

    It is the starting point for building up an object step by step with bind, which is covered in Building Objects:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<{
        name: "Alice";
        age: 30;
    }, never>
    result
    =
    import Result
    Result
    .
    function pipe<Result.Result<{}, never>, Result.Result<{
        name: "Alice";
    }, never>, Result.Result<{
        name: "Alice";
        age: 30;
    }, never>>(a: Result.Result<{}, never>, ab: (a: Result.Result<{}, never>) => Result.Result<{
        name: "Alice";
    }, never>, bc: (b: Result.Result<{
        name: "Alice";
    }, never>) => Result.Result<{
        name: "Alice";
        age: 30;
    }, never>): Result.Result<{
        name: "Alice";
        age: 30;
    }, never> (+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.@example
    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
      2,
      x => x + 1,
      x => x * 3,
      x => `Result: ${x}`
    );
    // Result: 9
    @categoryUtilities
    pipe
    (
    import Result
    Result
    .
    function do(): Result.Result<{}, never>
    export do

    Alias for succeed({}). Commonly used as a neutral base value in functional chains or monadic pipelines.

    @example
    import { Result } from '@praha/byethrow';
    
    const result = Result.do();
    // Result.Result<{}, never>
    @categoryCreators
    do
    (),
    import Result
    Result
    .
    function bind<"name", Result.Result<{}, never>, Result.Result<"Alice", never>>(name: "name", fn: (a: {}) => Result.Result<"Alice", never>): (result: Result.Result<{}, never>) => Result.Result<{
        name: "Alice";
    }, never> (+1 overload)

    Chains another Result -producing computation and merges its success value into the existing object under the specified key.

    • If the original result is a Failure , it is returned as-is.
    • If the next result is a Failure , it is returned as-is.
    • If both are Success , a new object is returned by shallow-merging: the original success object and { [name]: nextSuccess }.

    This is useful for building up objects in a compositional and type-safe way, especially in validation or data-fetching pipelines.

    @typeParamN - The key to assign the result of the fn computation.@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ name: 'Alice' }),
    Result.bind('age', (user) => Result.succeed(20)),
    );
    // { type: 'Success', value: { name: 1, age: 20 } }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.bind('age', (user) => Result.succeed(20)),
    );
    // { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ name: 'Alice' }),
    Result.bind('age', (user) => Result.fail('error')),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    bind
    ('name', () =>
    import Result
    Result
    .
    function succeed<"Alice">(value: "Alice"): Result.Result<"Alice", 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    ('Alice')),
    import Result
    Result
    .
    function bind<"age", Result.Result<{
        name: "Alice";
    }, never>, Result.Result<30, never>>(name: "age", fn: (a: {
        name: "Alice";
    }) => Result.Result<30, never>): (result: Result.Result<{
        name: "Alice";
    }, never>) => Result.Result<{
        name: "Alice";
        age: 30;
    }, never> (+1 overload)

    Chains another Result -producing computation and merges its success value into the existing object under the specified key.

    • If the original result is a Failure , it is returned as-is.
    • If the next result is a Failure , it is returned as-is.
    • If both are Success , a new object is returned by shallow-merging: the original success object and { [name]: nextSuccess }.

    This is useful for building up objects in a compositional and type-safe way, especially in validation or data-fetching pipelines.

    @typeParamN - The key to assign the result of the fn computation.@typeParamR1 - The input Result or ResultAsync .@typeParamR2 - The result type returned by fn.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ name: 'Alice' }),
    Result.bind('age', (user) => Result.succeed(20)),
    );
    // { type: 'Success', value: { name: 1, age: 20 } }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.bind('age', (user) => Result.succeed(20)),
    );
    // { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed({ name: 'Alice' }),
    Result.bind('age', (user) => Result.fail('error')),
    );
    // { type: 'Failure', error: 'error' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    bind
    ('age', () =>
    import Result
    Result
    .
    function succeed<30>(value: 30): Result.Result<30, 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.@example

    Synchronous Usage

    import { Result } from '@praha/byethrow';
    
    const result = Result.succeed(42);
    // Result.Result<42, never>
    @example

    With 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
    (30)),
    ); // { type: 'Success', value: { name: 'Alice', age: 30 } }

    References

    APIPurpose
    succeed(value)Create a success result
    fail(error)Create a failure result
    do()Create a success result holding an empty object