@lppedd/mini-result - v0.5.0
    Preparing search index...

    Interface Err<V, E>

    Represents a failed result containing an error of type E.

    interface Err<V, E> {
        error: E;
        catch<RV = V>(fn: (e: E) => Ok<RV, E>): Result<V | RV, E>;
        catch<RE = E>(fn: (e: E) => Err<V, RE>): Result<V, RE>;
        catch<RV = V, RE = E>(fn: (e: E) => Result<RV, RE>): Result<V | RV, RE>;
        catch<RV = V>(fn: (e: E) => NoResult<RV>): Result<V | RV, E>;
        catchAsync<RV = V>(
            fn: (e: E) => Promise<Ok<RV, E>>,
        ): AsyncResult<V | RV, E>;
        catchAsync<RE = E>(fn: (e: E) => Promise<Err<V, RE>>): AsyncResult<V, RE>;
        catchAsync<RV = V, RE = E>(
            fn: (e: E) => Promise<Result<RV, RE>>,
        ): AsyncResult<V | RV, RE>;
        catchAsync<RV = V>(
            fn: (e: E) => NoResult<RV> | Promise<NoResult<RV>>,
        ): AsyncResult<V | RV, E>;
        isErr(): this is Err<V, E>;
        isOk(): this is Ok<V, E>;
        map<RV = V>(fn: (v: V) => Ok<RV, E>): Result<RV, E>;
        map<RE = E>(fn: (v: V) => Err<V, RE>): Result<V, E | RE>;
        map<RV = V, RE = E>(fn: (v: V) => Result<RV, RE>): Result<RV, E | RE>;
        map<RV = V>(fn: (v: V) => NoResult<RV>): Result<RV, E>;
        mapAsync<RV = V>(fn: (v: V) => Promise<Ok<RV, E>>): AsyncResult<RV, E>;
        mapAsync<RE = E>(fn: (v: V) => Promise<Err<V, RE>>): AsyncResult<V, E | RE>;
        mapAsync<RV = V, RE = E>(
            fn: (v: V) => Promise<Result<RV, RE>>,
        ): AsyncResult<RV, E | RE>;
        mapAsync<RV = V>(
            fn: (v: V) => NoResult<RV> | Promise<NoResult<RV>>,
        ): AsyncResult<RV, E>;
        match<RV, RE = RV>(ok: (v: V) => RV, err: (e: E) => RE): RV | RE;
        tap(
            fn: ((v: V) => unknown) | undefined,
            fne?: (e: E) => unknown,
        ): Result<V, E>;
        unwrap(): V;
        unwrapOr<RV = V>(fn: (e: E) => RV): V | RV;
    }

    Type Parameters

    • V
    • E

      The error value type.

    Implements

    • IResult<V, E>
    Index

    Properties

    error: E

    Methods

    • Transforms the error value if this is an Err result, using a function that returns another Result.

      This method is useful for recovering from failures and flattening chains of results that operate on errors.

      If this is an Ok result, its success value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      Returns Result<V | RV, E>

    • Type Parameters

      • RE = E

      Parameters

      Returns Result<V, RE>

    • Type Parameters

      • RV = V
      • RE = E

      Parameters

      Returns Result<V | RV, RE>

    • Replaces the error value with an Ok result using a raw value.

      Equivalent to writing:

      result.catch((e) => Res.ok(value))
      

      If this is an Ok result, its success value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      • fn: (e: E) => NoResult<RV>

      Returns Result<V | RV, E>

    • Transforms the error value if this is an async Err result, using a function that returns another Result.

      This method is useful for recovering from failures and flattening chains of async results that operate on errors.

      If this is an async Ok result, its success value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      • fn: (e: E) => Promise<Ok<RV, E>>

      Returns AsyncResult<V | RV, E>

    • Type Parameters

      • RE = E

      Parameters

      Returns AsyncResult<V, RE>

    • Type Parameters

      • RV = V
      • RE = E

      Parameters

      Returns AsyncResult<V | RV, RE>

    • Replaces the error value with an Ok result using a raw value.

      Equivalent to writing:

      result.catchAsync((e) => Promise.resolve(Res.ok(value)))
      

      If this is an async Ok result, its success value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      • fn: (e: E) => NoResult<RV> | Promise<NoResult<RV>>

      Returns AsyncResult<V | RV, E>

    • Returns true if this is an Err result.

      Returns this is Err<V, E>

    • Returns true if this is an Ok result.

      Returns this is Ok<V, E>

    • Transforms the success value if this is an Ok result, using a function that returns another Result.

      This method is useful to flatten chained results.

      If this is an Err result, its error value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      Returns Result<RV, E>

    • Type Parameters

      • RE = E

      Parameters

      Returns Result<V, E | RE>

    • Type Parameters

      • RV = V
      • RE = E

      Parameters

      Returns Result<RV, E | RE>

    • Transforms the success value if this is an Ok result, using a raw value.

      Example:

      const r = getCores().map((n) => n * 3); // Ok(n * 3)
      

      If this is an Err result, its error value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      • fn: (v: V) => NoResult<RV>

      Returns Result<RV, E>

    • Transforms the success value if this is an async Ok result, using a function that returns another Result.

      This method is useful to flatten chained async results.

      If this is an async Err result, its error value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      • fn: (v: V) => Promise<Ok<RV, E>>

      Returns AsyncResult<RV, E>

    • Type Parameters

      • RE = E

      Parameters

      Returns AsyncResult<V, E | RE>

    • Type Parameters

      • RV = V
      • RE = E

      Parameters

      Returns AsyncResult<RV, E | RE>

    • Transforms the success value if this is an async Ok result, using a raw value.

      Example:

      // getCores(): Promise<Result<number, Error>
      // adjustCores(n): Promise<number>
      const r = Res.from(getCores()).mapAsync((n) => adjustCores(n));
      // r: AsyncResult<number, Error>

      If this is an Err result, its error value is preserved unchanged.

      Type Parameters

      • RV = V

      Parameters

      • fn: (v: V) => NoResult<RV> | Promise<NoResult<RV>>

      Returns AsyncResult<RV, E>

    • Exhaustively handles both the Ok and Err result variants.

      Example:

      const r = result.match(
      (v) => // Ok branch
      (e) => // Err branch
      )

      Type Parameters

      Parameters

      • ok: (v: V) => RV

        The function called when the result is Ok.

      • err: (e: E) => RE

        The function called when the result is Err.

      Returns RV | RE

    • Invokes the fnv function with the success value if this is an Ok result, or the fne function if this is an Err result.

      The result is returned unchanged.

      Parameters

      • fn: ((v: V) => unknown) | undefined
      • Optionalfne: (e: E) => unknown

      Returns Result<V, E>

    • Returns the success value if this is an Ok result, or throws if this is an Err result.

      Returns V

      If this is an Err result, which does not have a value.

    • Returns the success value if this is an Ok result, or falls back to the value produced by the given function if this is an Err result.

      Type Parameters

      • RV = V

      Parameters

      • fn: (e: E) => RV

      Returns V | RV