For AI agents: the complete documentation index is available at /byethrow/ja/llms.txt, the full documentation bundle is available at /byethrow/ja/llms-full.txt, and this page is available as Markdown at /byethrow/ja/guide/tutorial/basics/creating-results.md.
  • 日本語
  • Result を作成する

    Result の形がわかったところで、ライブラリのヘルパー関数を使って作成してみましょう。

    succeed で成功を作成する

    succeed は Success を作成します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // 値あり 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);
    // 型: Result.Result<42, never> // 値なし(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
    ();
    // 型: Result.Result<void, never>

    この結果が失敗になることはないため、エラーの型は never になります。 void の成功は、保存や削除のように意味のある戻り値を持たない操作で便利です。

    fail で失敗を作成する

    fail は Failure を作成します。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    // エラー値あり 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');
    // 型: Result.Result<never, 'NotFound'> // 値なし(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
    ();
    // 型: Result.Result<never, void>

    エラーにはどんな値でも使えます。実際のアプリケーションでは、「カスタムエラー」で説明しているように、専用のエラークラスを使うことをおすすめします。

    Info

    上の例のとおり、succeed と fail は 42 や 'NotFound' のようなリテラル型を number や string に広げずにそのまま保ちます。 そのため、'NotFound' のような文字列リテラルのエラーを互いに区別しやすくなります。 より広い型にしたい場合は、後述のように関数の戻り値の型を注釈してください。

    関数から Result を返す

    succeed と fail の最も一般的な使い方は、return と 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');
    // 型: Result.Result<number, 'InvalidAge'>

    TypeScript は、return 文から成功値の型とエラーの型の両方を推論します。 戻り値の型を明示的に注釈することもでき、エクスポートする関数ではそれが良い習慣です。

    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 と fail は Promise を自動的に await しません。 非同期の値がある場合は先に await するか、戻り値の型が ResultAsync の async 関数を使ってください。

    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
    );
    };

    do でパイプラインを開始する

    do は空のオブジェクトを持つ Success を作成します。

    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
    ();
    // 型: Result.Result<{}, never>

    これは、bind を使ってオブジェクトを一歩ずつ組み立てるときの出発点になります。詳しくは「オブジェクトを構築する」で説明します。

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

    リファレンス

    API用途
    succeed(value)成功の Result を作成する
    fail(error)失敗の Result を作成する
    do()空のオブジェクトを持つ成功の Result を作成する