Ok is a type that represents a successful result.

Type Parameters

  • T

Implements

Constructors

  • Creates an Ok.

    Type Parameters

    • T

    Parameters

    • value: T

      The value to be wrapped.

    Returns Ok<T>

    const result = new Ok(42);
    

Methods

  • Returns a Result. If this Result is an Ok, the function returns passed Result.

    Type Parameters

    • U
    • E

    Parameters

    • res: Result<U, E>

      The Result to be returned.

    Returns Result<U, E>

    A Result that is the result of applying the given function to the value of this Result.

    const result1 = new Ok(42);
    const result2 = new Ok('hello');
    const combined = result1.and(result2); // combined is result2
  • Returns a new Result that is the result of applying the given function to the value of this Result.

    Type Parameters

    • U
    • E2

    Parameters

    • fn: ((value: T) => Result<U, E2>)

      The function to be applied to the value of this Result.

    Returns Result<U, E2>

    A Result that is the result of applying the given function to the value of this Result.

    const result = new Ok(42);
    const newResult = result.andThen(value => new Ok(value + 1)); // newResult is Ok(43)
  • Returns true if this Result is an Err, false otherwise.

    Returns this is Err<unknown>

    false as this Result is an Ok.

    const result = new Ok(42);
    console.log(result.isError()); // false
  • Returns true if this Result is an Ok, false otherwise.

    Returns this is Ok<T>

    true if this Result is an Ok, false otherwise.

    const result = new Ok(42);
    console.log(result.isOk()); // true
  • Returns a new Result that is the result of applying the given function to the value of this Result.

    Type Parameters

    • U

    Parameters

    • fn: ((value: T) => U)

      The function to be applied to the value of this Result.

        • (value): U
        • Parameters

          • value: T

          Returns U

    Returns Result<U, never>

    A Result that is the result of applying the given function to the value of this Result.

    const result = new Ok(42);
    const mappedResult = result.map(value => value.toString()); // mappedResult is Ok('42')
  • Returns a new Result that is the result of applying the given function to the error of this Result.

    Type Parameters

    • E2

    Parameters

    • _: ((error: never) => E2)

      The function to be applied to the error of this Result.

        • (error): E2
        • Parameters

          • error: never

          Returns E2

    Returns Result<T, E2>

    This Result as it is an Ok.

    const result = new Ok(42);
    const mappedErrorResult = result.mapError(error => 'new error'); // mappedErrorResult is still Ok(42)
  • Returns the value of this Result.

    Returns T

    The value of this Result.

    const result = new Ok(42);
    console.log(result.unwrap()); // 42
  • Throws an error as this Result is an Ok.

    Returns never

    If this Result is an Ok.

    const result = new Ok(42);
    result.unwrapError(); // throws Error
  • Returns the value of this Result.

    Parameters

    • _: T

      The value to be returned if this Result is an Err.

    Returns T

    The value of this Result.

    const result = new Ok(42);
    console.log(result.unwrapOr(0)); // 42