@dbidwell94/ts-utils
    Preparing search index...

    Interface ResultUtils<T, E>

    interface ResultUtils<T, E extends Error = Error> {
        andThen<NewT>(fn: (value: T) => Result<NewT, E>): Result<NewT, E>;
        err(): Option<E>;
        expect(message: string): T;
        inspect(callback: (value: T) => void): Result<T>;
        isError(): this is Failure<E>;
        isOk(): this is Success<T>;
        mapErr<NewE extends Error>(fn: (error: E) => NewE): Result<T, NewE>;
        mapOk<NewT>(fn: (value: T) => NewT): Result<NewT, E>;
        ok(): Option<T>;
        unwrap(): T;
        unwrapOr(defaultValue: T): T;
        unwrapOrElse(error: E): T;
    }

    Type Parameters

    • T
    • E extends Error = Error
    Index

    Methods

    • Maps a Result<T, E> to Result<NewT, E> by applying a function to a contained Success<T> value, leaving an Error<E> value untouched. The resulting Result<NewT, E> is then flattened to avoid nested Result types.

      Type Parameters

      • NewT

      Parameters

      • fn: (value: T) => Result<NewT, E>

        The function to apply to the inner value

      Returns Result<NewT, E>

    • Useful if you do not care if there is a value, you only want to know if there is an error.

      Returns Option<E>

      An Option<E> if the inner type was a Failure<E>, otherwise None

      Option

    • Unwraps the inner value of this Result<T>. If this Result was an error variant, then an error will be thrown with the message specified.

      Parameters

      • message: string

        The message to set on an Error if this Result is not a Success variant.

      Returns T

      The inner value of this Result if this Result is a Success variant.

      • If this result was a Failure variant.
    • Peek into the inner value of this Result, and if isOk() then the callback function will be run.

      Parameters

      • callback: (value: T) => void

        the callback to run if this Result isOk

      Returns Result<T>

      this Result<T>

    • Utility function to determine if the inner type is a Failure<E>

      Returns this is Failure<E>

      true if the inner type is a Failure<E>, otherwise false

    • Utility function to determine if the inner type is a Success<T>

      Returns this is Success<T>

      true if the inner type is a Success<T>, otherwise false

    • Maps a Result<T, E> to Result<T, NewE> by applying a function to a contained Error<E> value, leaving a Success<T> value untouched.

      Type Parameters

      • NewE extends Error

      Parameters

      • fn: (error: E) => NewE

        The function to apply to the inner error

      Returns Result<T, NewE>

    • Maps a Result<T, E> to Result<NewT, E> by applying a function to a contained Success<T> value, leaving an Error<E> value untouched.

      Type Parameters

      • NewT

      Parameters

      • fn: (value: T) => NewT

        The function to apply to the inner value

      Returns Result<NewT, E>

    • Useful if you do not care if there is an error, you only want to know if there is a value.

      Returns Option<T>

      An Option<T> if the inner type was a Success<T>, otherwise None

      Option

    • Unwraps the Result<T, E>. Throws the inner type if it was a Failure<E>

      Returns T

      The inner type if it was a Success<T>

      • If the inner type was a Failure<E>
    • Unwraps the Result<T, E>. Returns the provided default value if the inner type was a Failure<E>

      Parameters

      • defaultValue: T

        The value to return if the inner type was a Failure<E>

      Returns T

      The inner type if it was a Success<T>, otherwise the provided default value

    • Unwraps the Result<T, E>. Throws the provided error if the inner type was a Failure<E>

      Parameters

      • error: E

        The error to throw if the inner type was a Failure<E>

      Returns T

      The inner type if it was a Success<T>

      • If the inner type was a Failure<E>