• English
  • Function: isFailure()

    isFailure<R>(result): result is Extract<R, { type: "Failure" }>

    Type guard to check if a Result is a Failure.

    Type Parameters

    R

    R extends Result<unknown, unknown>

    The type of the result to check.

    Parameters

    result

    R

    The Result to check.

    Returns

    result is Extract<R, { type: "Failure" }>

    true if the result is a Failure, otherwise false.

    Example

    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> = {
    type: "Failure"
    type
    : 'Failure',
    error: string
    error
    : 'Something went wrong' };
    if (
    import Result
    Result
    .
    const isFailure: <Result.Failure<string>>(result: Result.Failure<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.Failure<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
    (
    const result: Result.Failure<string>
    result
    .
    error: string
    error
    ); // Safe access to error
    }