Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface IFailable<Result, Error>

Type that represents a failable computation. Is parametrized over the result type and the error type.

Type parameters

  • Result

  • Error

Hierarchy

  • IFailable

Index

Properties

Methods

Properties

result

result: IFailableResult<Result, Error>

Return an object containing the result of the computation from which the value or error can be extracted using .isError check. Useful as an alternative to .match

example

const failable: IFailable<number, string> = ...;
if (failable.result.isError) {
  // .error can be accessed inside this block
  console.log(failable.result.error);
} else {
  // .value can be accessed inside this block
  console.log(failable.result.value);
}

Methods

flatMap

  • flatMap<R2, E2>(f: function): IFailable<R2, Error | E2>
  • Chain another computation to this IFailable that takes the result value of this IFailable and returns a new IFailable (possibly of a different type). The chained computation must be an IFailable whose error type is a subset of this IFailable's error type. If not, you can call .mapError on it to convert it's error into a type compatible with this IFailable.

    This method allows you to chain arbitrary failable computations dependent on the results of previous ones in the chain that "short circuit" in case of the first error.

    example
    
    const computation1: () => Failable<number, ERROR> = ...;
    const computation2: (x: int) => Failable<string, ERROR> = ...;
    const result: Failable<string, ERROR> = computation1().flatMap(x => computation2(x))
    

    Type parameters

    • R2

    • E2: Error

    Parameters

    • f: function

      Function that takes the success value of this IFailable and returns another IFailable (possibly of another type)

    Returns IFailable<R2, Error | E2>

map

  • Transform an IFailable<R, E> into an IFailable<R2, E> by applying the given function to the result value in case of success. Has no effect in case this is a failure.

    example
    
    const numStr: Failable<string, string> = ...;
    const parsed: Failable<number, string> = numStr.map(parseInt); // or numStr.map(s => parseInt(s))
    

    Type parameters

    • R2

    Parameters

    • f: function

      Function that transforms a success value.

        • (r: Result): R2
        • Parameters

          • r: Result

          Returns R2

    Returns IFailable<R2, Error>

mapError

  • mapError<E2>(f: function): IFailable<Result, E2>
  • Transform the error value of an IFailable using the given function. Has no effect if the IFailable was a success.

    example
    
    const result: Failable<number, string> = ...;
    const withErrorCode: Failable<number, number> = result.mapError(getErrorCode)
    

    Type parameters

    • E2

    Parameters

    • f: function

      Function for transforming the error value

        • (e: Error): E2
        • Parameters

          • e: Error

          Returns E2

    Returns IFailable<Result, E2>

match

  • Pattern match over this IFailable by supplying a success and failure functions. Both cases must return a value of type T

    example
    
    const result: Failable<number, string> = ...;
    const num = result.match({
      success: x => x,
      failure: err => {
        console.log(err);
        return 0;
      }
    }); // num = x if result was successful, otherwise 0
    

    Type parameters

    • T

    Parameters

    Returns T

Generated using TypeDoc