• 日本語
  • Result を確認する

    Result を作成または受け取った後、その内容にアクセスする前に、それが成功か失敗かを判定する必要があります。 このセクションでは、これを安全に行うための型ガード関数について説明します。

    isSuccess で成功をチェックする

    isSuccess 関数は、ResultSuccess かどうかをチェックする型ガード関数です。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, string>
    result
    :
    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: <42>(value: 42) => Result.Result<42, never> (+1 overload)
    succeed
    (42);
    if (
    import Result
    Result
    .
    const isSuccess: <Result.Result<number, string>>(result: Result.Result<number, string>) => result is Result.Success<number>

    Type guard to check if a Result is a Success .

    @function@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Success , otherwise false.@example
    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = { type: 'Success', value: 10 };
    if (Result.isSuccess(result)) {
      console.log(result.value); // Safe access to value
    }
    @categoryType Guards
    isSuccess
    (
    const result: Result.Result<number, string>
    result
    )) {
    // TypeScript はここで result が Success<number> であることを知っている
    var console: Console
    console
    .
    Console.log(...data: any[]): void

    The console.log() static method outputs a message to the console.

    MDN Reference

    log
    (
    const result: Result.Success<number>
    result
    .
    value: number
    value
    ); // 42
    }

    型の絞り込み

    isSuccess 関数を使用することで、型を絞り込み value プロパティへ安全にアクセス出来るようになります。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const processResult: (result: Result.Result<string, Error>) => string
    processResult
    = (
    result: Result.Result<string, Error>
    result
    :
    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
    <string, Error>) => {
    if (
    import Result
    Result
    .
    const isSuccess: <Result.Result<string, Error>>(result: Result.Result<string, Error>) => result is Result.Success<string>

    Type guard to check if a Result is a Success .

    @function@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Success , otherwise false.@example
    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = { type: 'Success', value: 10 };
    if (Result.isSuccess(result)) {
      console.log(result.value); // Safe access to value
    }
    @categoryType Guards
    isSuccess
    (
    result: Result.Result<string, Error>
    result
    )) {
    // ✅ TypeScript は `value` が存在することを知っている return
    result: Result.Success<string>
    result
    .
    value: string
    value
    .
    String.toUpperCase(): string

    Converts all the alphabetic characters in a string to uppercase.

    toUpperCase
    ();
    } // ✅ TypeScript はここで `error` が存在することを知っている return `Error: ${
    result: Result.Failure<Error>
    result
    .
    error: Error
    error
    .
    Error.message: string
    message
    }`;
    };

    isFailure で失敗をチェックする

    isFailure 関数は、ResultFailure かどうかをチェックする型ガード関数です。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const result: Result.Result<number, string>
    result
    :
    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 fail: <"Something went wrong">(error: "Something went wrong") => Result.Result<never, "Something went wrong"> (+1 overload)
    fail
    ('Something went wrong');
    if (
    import Result
    Result
    .
    const isFailure: <Result.Result<number, string>>(result: Result.Result<number, string>) => result is Result.Failure<string>

    Type guard to check if a Result is a Failure .

    @function@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Failure , otherwise false.@example
    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = { type: 'Failure', error: 'Something went wrong' };
    if (Result.isFailure(result)) {
      console.error(result.error); // Safe access to error
    }
    @categoryType Guards
    isFailure
    (
    const result: Result.Result<number, string>
    result
    )) {
    // TypeScript はここで result が Failure<string> であることを知っている
    var console: Console
    console
    .
    Console.log(...data: any[]): void

    The console.log() static method outputs a message to the console.

    MDN Reference

    log
    (
    const result: Result.Failure<string>
    result
    .
    error: string
    error
    ); // "Something went wrong"
    }

    早期リターン

    よくあるパターンは、最初に失敗をチェックして早期リターンすることです。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const handleUserLookup: (result: Result.Result<User, string>) => User | null
    handleUserLookup
    = (
    result: Result.Result<User, string>
    result
    :
    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
    <
    type User = {
        id: number;
        name: string;
    }
    User
    , string>) => {
    if (
    import Result
    Result
    .
    const isFailure: <Result.Result<User, string>>(result: Result.Result<User, string>) => result is Result.Failure<string>

    Type guard to check if a Result is a Failure .

    @function@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Failure , otherwise false.@example
    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = { type: 'Failure', error: 'Something went wrong' };
    if (Result.isFailure(result)) {
      console.error(result.error); // Safe access to error
    }
    @categoryType Guards
    isFailure
    (
    result: Result.Result<User, string>
    result
    )) {
    var console: Console
    console
    .
    Console.error(...data: any[]): void

    The console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.

    MDN Reference

    error
    ('Failed to find user:',
    result: Result.Failure<string>
    result
    .
    error: string
    error
    );
    return null; } // 早期リターン後、TypeScript は result が Success であることを知っている return
    result: Result.Success<User>
    result
    .
    value: User
    value
    ;
    };

    isResult で値が Result かどうかをチェックする

    isResult 関数は、値が Result であるかどうかをチェックする型ガード関数です。 任意の値が Result かどうかをチェックする必要がある場合に便利です。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const maybeResult: unknown
    maybeResult
    : unknown =
    import Result
    Result
    .
    const succeed: <42>(value: 42) => Result.Result<42, never> (+1 overload)
    succeed
    (42);
    if (
    import Result
    Result
    .
    const isResult: <unknown, unknown>(result: unknown) => result is Result.Result<unknown, unknown>

    Type guard to check if a value is a Result .

    @function@typeParamT - The type of the success value.@typeParamE - The type of the error value.@paramresult - The value to check.@returnstrue if the value is a Result, otherwise false.@example
    import { Result } from '@praha/byethrow';
    
    const value: unknown = { type: 'Success', value: 42 };
    if (Result.isResult(value)) {
      // value is now typed as Result<unknown, unknown>
      console.log(value.type); // 'Success' or 'Failure'
    }
    @categoryType Guards
    isResult
    (
    const maybeResult: unknown
    maybeResult
    )) {
    // TypeScript は maybeResult が Result<unknown, unknown> であることを知っている if (
    import Result
    Result
    .
    const isSuccess: <Result.Result<unknown, unknown>>(result: Result.Result<unknown, unknown>) => result is Result.Success<unknown>

    Type guard to check if a Result is a Success .

    @function@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Success , otherwise false.@example
    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = { type: 'Success', value: 10 };
    if (Result.isSuccess(result)) {
      console.log(result.value); // Safe access to value
    }
    @categoryType Guards
    isSuccess
    (
    const maybeResult: Result.Result<unknown, unknown>
    maybeResult
    )) {
    var console: Console
    console
    .
    Console.log(...data: any[]): void

    The console.log() static method outputs a message to the console.

    MDN Reference

    log
    (
    const maybeResult: Result.Success<unknown>
    maybeResult
    .
    value: unknown
    value
    );
    } }

    汎用ユーティリティ

    よくあるパターンは、様々な型を受け入れて Result を特別に扱う関数を書くことです。

    import { 
    import Result
    Result
    } from '@praha/byethrow';
    const
    const stringify: (value: unknown) => string
    stringify
    = (
    value: unknown
    value
    : unknown): string => {
    if (
    import Result
    Result
    .
    const isResult: <unknown, unknown>(result: unknown) => result is Result.Result<unknown, unknown>

    Type guard to check if a value is a Result .

    @function@typeParamT - The type of the success value.@typeParamE - The type of the error value.@paramresult - The value to check.@returnstrue if the value is a Result, otherwise false.@example
    import { Result } from '@praha/byethrow';
    
    const value: unknown = { type: 'Success', value: 42 };
    if (Result.isResult(value)) {
      // value is now typed as Result<unknown, unknown>
      console.log(value.type); // 'Success' or 'Failure'
    }
    @categoryType Guards
    isResult
    (
    value: unknown
    value
    )) {
    if (
    import Result
    Result
    .
    const isSuccess: <Result.Result<unknown, unknown>>(result: Result.Result<unknown, unknown>) => result is Result.Success<unknown>

    Type guard to check if a Result is a Success .

    @function@typeParamR - The type of the result to check.@paramresult - The Result to check.@returnstrue if the result is a Success , otherwise false.@example
    import { Result } from '@praha/byethrow';
    
    const result: Result.Result<number, string> = { type: 'Success', value: 10 };
    if (Result.isSuccess(result)) {
      console.log(result.value); // Safe access to value
    }
    @categoryType Guards
    isSuccess
    (
    value: Result.Result<unknown, unknown>
    value
    )) {
    return `Success: ${
    var JSON: JSON

    An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

    JSON
    .
    JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

    Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

    @paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
    stringify
    (
    value: Result.Success<unknown>
    value
    .
    value: unknown
    value
    )}`;
    } return `Failure: ${
    var JSON: JSON

    An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

    JSON
    .
    JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

    Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

    @paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
    stringify
    (
    value: Result.Failure<unknown>
    value
    .
    error: unknown
    error
    )}`;
    } return
    var JSON: JSON

    An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.

    JSON
    .
    JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)

    Converts a JavaScript value to a JavaScript Object Notation (JSON) string.

    @paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
    stringify
    (
    value: unknown
    value
    );
    };

    リファレンス

    関数目的
    isSuccess(result)Result が Success かチェック
    isFailure(result)Result が Failure かチェック
    isResult(value)値が Result かどうかをチェック