• English
  • Checking Results

    After creating or receiving a Result, you need to determine whether it's a success or failure before accessing its contents. This section covers the type guard functions that help you do this safely.

    Checking for Success with isSuccess

    The isSuccess function is a type guard that checks if a Result is a Success:

    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 knows result is Success<number> here
    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
    }

    Type Narrowing

    The power of isSuccess is that it narrows the type, giving you safe access to the value property:

    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 knows `value` exists return
    result: Result.Success<string>
    result
    .
    value: string
    value
    .
    String.toUpperCase(): string

    Converts all the alphabetic characters in a string to uppercase.

    toUpperCase
    ();
    } // ✅ TypeScript knows `error` exists here return `Error: ${
    result: Result.Failure<Error>
    result
    .
    error: Error
    error
    .
    Error.message: string
    message
    }`;
    };

    Checking for Failure with isFailure

    The isFailure function is the opposite—it checks if a Result is a Failure:

    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 knows result is Failure<string> here
    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"
    }

    Early Return

    A common pattern is to check for failure first and return early:

    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; } // After the early return, TypeScript knows result is Success return
    result: Result.Success<User>
    result
    .
    value: User
    value
    ;
    };

    Checking if a Value is a Result with isResult

    Sometimes you need to check if an arbitrary value is a Result. The isResult function helps with this:

    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 knows maybeResult is 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
    );
    } }

    Generic Utilities

    A common pattern is to write functions that accept various types and handle Results specially:

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

    References

    FunctionPurpose
    isSuccess(result)Check if result is Success
    isFailure(result)Check if result is Failure
    isResult(value)Check if value is a Result