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/start/introduction.md.
  • English
  • Introduction

    @praha/byethrow is a lightweight, tree-shakable library that brings the Result type to JavaScript and TypeScript. Instead of throwing exceptions, functions return a value that is either a success or a failure. The possibility of failure becomes part of the type, so the compiler can make sure it is handled.

    At a Glance

    With throw, a function's signature tells you nothing about how it can fail:

    // Can this throw? Which errors? The signature doesn't say.
    const 
    const findUser: (id: string) => Promise<User>
    findUser
    = async (
    id: string
    id
    : string):
    interface Promise<T>

    Represents the completion of an asynchronous operation

    Promise
    <
    type User = {
        id: string;
        name: string;
    }
    User
    > => {
    // ... };

    With byethrow, every expected failure appears in the return type, and results can be composed step by step without try/catch:

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const validateId: (id: string) => Result.Failure<InvalidIdError> | Result.Success<string>
    validateId
    = (
    id: string
    id
    : string) => {
    if (!
    id: string
    id
    .
    String.startsWith(searchString: string, position?: number): boolean

    Returns true if the sequence of elements of searchString converted to a String is the same as the corresponding elements of this object (converted to a String) starting at position. Otherwise returns false.

    startsWith
    ('u')) {
    return
    import Result
    Result
    .
    function fail<InvalidIdError>(error: InvalidIdError): Result.Result<never, InvalidIdError> (+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
    (new
    constructor InvalidIdError(message?: string, options?: ErrorOptions): InvalidIdError (+1 overload)
    InvalidIdError
    ());
    } 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
    (
    id: string
    id
    );
    }; const
    const findUser: (id: string) => Result.ResultAsync<User, UserNotFoundError>
    findUser
    = async (
    id: string
    id
    : string):
    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
    <
    type User = {
        id: string;
        name: string;
    }
    User
    ,
    class UserNotFoundError
    UserNotFoundError
    > => {
    const
    const user: User | undefined
    user
    = await
    const db: {
        findUser: (id: string) => Promise<User | undefined>;
    }
    db
    .
    findUser: (id: string) => Promise<User | undefined>
    findUser
    (
    id: string
    id
    );
    if (!
    const user: User | undefined
    user
    ) {
    return
    import Result
    Result
    .
    function fail<UserNotFoundError>(error: UserNotFoundError): Result.Result<never, UserNotFoundError> (+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
    (new
    constructor UserNotFoundError(message?: string, options?: ErrorOptions): UserNotFoundError (+1 overload)
    UserNotFoundError
    ());
    } return
    import Result
    Result
    .
    function succeed<User>(value: User): Result.Result<User, 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 user: User
    user
    );
    }; const
    const result: Result.Result<User, InvalidIdError | UserNotFoundError>
    result
    = await
    import Result
    Result
    .
    function pipe<Result.Result<"u123", never>, Result.Result<string, InvalidIdError>, Result.ResultAsync<User, InvalidIdError | UserNotFoundError>>(a: Result.Result<"u123", never>, ab: (a: Result.Result<"u123", never>) => Result.Result<string, InvalidIdError>, bc: (b: Result.Result<string, InvalidIdError>) => Result.ResultAsync<User, InvalidIdError | UserNotFoundError>): Result.ResultAsync<...> (+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 succeed<"u123">(value: "u123"): Result.Result<"u123", 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
    ('u123'),
    import Result
    Result
    .
    function andThen<Result.Result<"u123", never>, Result.Failure<InvalidIdError> | Result.Success<string>>(fn: (a: "u123") => Result.Failure<InvalidIdError> | Result.Success<string>): (result: Result.Result<"u123", never>) => Result.Result<string, InvalidIdError> (+1 overload)

    Chains the next computation using the success value of a Result or ResultAsync . If the original result is a Failure , 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.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // result: { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.fail('error: ' + x)),
    );
    // result: { type: 'Failure', error: 'error: 3' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    andThen
    (
    const validateId: (id: string) => Result.Failure<InvalidIdError> | Result.Success<string>
    validateId
    ),
    import Result
    Result
    .
    function andThen<Result.Result<string, InvalidIdError>, Result.ResultAsync<User, UserNotFoundError>>(fn: (a: string) => Result.ResultAsync<User, UserNotFoundError>): (result: Result.Result<string, InvalidIdError>) => Result.ResultAsync<User, InvalidIdError | UserNotFoundError> (+1 overload)

    Chains the next computation using the success value of a Result or ResultAsync . If the original result is a Failure , 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.@example

    Success Case

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // { type: 'Success', value: 6 }
    @example

    Failure Case (input is a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.fail('error'),
    Result.andThen((x) => Result.succeed(x * 2)),
    );
    // result: { type: 'Failure', error: 'error' }
    @example

    Failure Case (function returns a Failure)

    import { Result } from '@praha/byethrow';
    
    const result = Result.pipe(
    Result.succeed(3),
    Result.andThen((x) => Result.fail('error: ' + x)),
    );
    // result: { type: 'Failure', error: 'error: 3' }
    @seepipe - It is recommended to use this function with the pipe function for better readability and composability.@categoryCombinators
    andThen
    (
    const findUser: (id: string) => Result.ResultAsync<User, UserNotFoundError>
    findUser
    ),
    ); // Type: Result.Result<User, InvalidIdError | UserNotFoundError>

    Features

    • Tree-shakable: Every function is an independent export, so your bundle only includes what you use.
    • Plain objects: A Result is a plain object, not a class instance. It is easy to log, serialize, and debug.
    • One API for sync and async: Every function accepts both Result<T, E> and Promise<Result<T, E>>.
    • Composable: Build readable pipelines with pipe and combinators such as map, andThen, and andThrough.
    • Focused: A small set of Result-centric functions, without aliases or confusing variants.
    • Fully type-safe: Every function is covered by type tests, so success and error types are inferred precisely.

    When to Use byethrow

    byethrow works best for failures that are part of your application's normal behavior, such as:

    • API calls that can fail because of network or server errors
    • Input validation where you want to report one or all problems to the user
    • File operations that can fail because of missing files or permissions
    • Parsers that must reject invalid input gracefully
    • Business rules such as "the post has already been deleted" or "the user has no permission"

    You don't need to wrap every possible error in a Result. Truly unexpected errors, such as a lost database connection, can still be thrown and handled by your infrastructure. See Result vs throw for how to draw that line.

    Next Steps

    • Why byethrow?: The problems byethrow solves and how it compares to other approaches
    • Quick Start: Install the package and write your first Result-based code
    • Tutorial: Learn every function step by step