Class OkImpl<T>

Contains the success value

Type Parameters

  • T

Implements

Constructors

  • Type Parameters

    • T

    Parameters

    • val: T

    Returns OkImpl<T>

Methods

  • Helper function if you know you have an Ok and T is iterable

    Returns Iterator<T extends Iterable<U>
        ? U
        : never, any, undefined>

  • Calls mapper if the result is Ok, otherwise returns the Err value of self. This function can be used for control flow based on Result values.

    Type Parameters

    • T2

    Parameters

    • mapper: ((val) => Ok<T2>)
        • (val): Ok<T2>
        • Parameters

          • val: T

          Returns Ok<T2>

    Returns Ok<T2>

  • Type Parameters

    • E2

    Parameters

    • mapper: ((val) => Err<E2>)
        • (val): Err<E2>
        • Parameters

          • val: T

          Returns Err<E2>

    Returns Result<T, E2>

  • Type Parameters

    • T2

    • E2

    Parameters

    • mapper: ((val) => Result<T2, E2>)

    Returns Result<T2, E2>

  • Parameters

    • _val: unknown

    Returns T

    See

    unwrapOr

    Deprecated

    in favor of unwrapOr

  • Returns the contained Ok value, if exists. Throws an error if not.

    The thrown error's [cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) is set to value contained in Err`.

    If you know you're dealing with Ok and the compiler knows it too (because you tested isOk() or isErr()) you should use value instead. While Ok's expect() and value will both return the same value using value is preferable because it makes it clear that there won't be an exception thrown on access.

    Parameters

    • _msg: string

      the message to throw if no Ok value.

    Returns T

  • Returns the contained Err value, if exists. Throws an error if not.

    Parameters

    • msg: string

      the message to throw if no Err value.

    Returns never

  • Maps a Result<T, E> to Result<U, E> by applying a function to a contained Ok value, leaving an Err value untouched.

    This function can be used to compose the results of two functions.

    Type Parameters

    • T2

    Parameters

    • mapper: ((val) => T2)
        • (val): T2
        • Parameters

          • val: T

          Returns T2

    Returns Ok<T2>

  • Maps a Result<T, E> to Result<T, F> by applying a function to a contained Err value, leaving an Ok value untouched.

    This function can be used to pass through a successful result while handling an error.

    Parameters

    • _mapper: unknown

    Returns Ok<T>

  • Maps a Result<T, E> to Result<U, E> by either converting T to U using mapper (in case of Ok) or using the default_ value (in case of Err).

    If default is a result of a function call consider using mapOrElse instead, it will only evaluate the function when needed.

    Type Parameters

    • U

    Parameters

    • _default_: U
    • mapper: ((val) => U)
        • (val): U
        • Parameters

          • val: T

          Returns U

    Returns U

  • Maps a Result<T, E> to Result<U, E> by either converting T to U using mapper (in case of Ok) or producing a default value using the default function (in case of Err).

    Type Parameters

    • U

    Parameters

    • _default_: ((_error) => U)
        • (_error): U
        • Parameters

          • _error: never

          Returns U

    • mapper: ((val) => U)
        • (val): U
        • Parameters

          • val: T

          Returns U

    Returns U

  • Returns Ok() if we have a value, otherwise returns other.

    other is evaluated eagerly. If other is a result of a function call try orElse() instead – it evaluates the parameter lazily.

    Type Parameters

    • E2

    Parameters

    Returns Result<T, E2>

    Example

    Ok(1).or(Ok(2)) // => Ok(1)
    Err('error here').or(Ok(2)) // => Ok(2)
  • Returns Ok() if we have a value, otherwise returns the result of calling other().

    other() is called only when needed and is passed the error value in a parameter.

    Type Parameters

    • E2

    Parameters

    • _other: ((error) => Result<T, E2>)
        • (error): Result<T, E2>
        • Parameters

          • error: never

          Returns Result<T, E2>

    Returns Result<T, E2>

    Example

    Ok(1).orElse(() => Ok(2)) // => Ok(1)
    Err('error').orElse(() => Ok(2)) // => Ok(2)
  • Returns the contained Ok value, but never throws. Unlike unwrap(), this method doesn't throw and is only callable on an Ok

    Therefore, it can be used instead of unwrap() as a maintainability safeguard that will fail to compile if the error type of the Result is later changed to an error that can actually occur.

    (this is the into_ok() in rust)

    Returns T

  • Converts from Result<T, E> to Option<T>, discarding the error if any

    Similar to rust's ok method

    Returns Option<T>

  • Returns string

  • Returns the contained Ok value. Because this function may throw, its use is generally discouraged. Instead, prefer to handle the Err case explicitly.

    If you know you're dealing with Ok and the compiler knows it too (because you tested isOk() or isErr()) you should use value instead. While Ok's unwrap() and value will both return the same value using value is preferable because it makes it clear that there won't be an exception thrown on access.

    Throws if the value is an Err, with a message provided by the Err's value and [`cause'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) set to the value.

    Returns T

  • Returns the contained Ok value or a provided default.

    (This is the unwrap_or in rust)

    Parameters

    • _val: unknown

    Returns T

Properties

value: T
EMPTY: OkImpl<void>