Interface Result<T, E>

The Result interface representing a success or an error. A Result is either Ok, holding a success value, or Err, holding an error value.

interface Result<T, E> {
    type: symbol;
    andThen<U>(fn): Result<U, E>;
    err(): Option<E>;
    isErr(): this is ErrResult<T, E>;
    isOk(): this is OkResult<T, E>;
    map<U>(fn): Result<U, E>;
    mapErr<F>(fn): Result<T, F>;
    match<U>(fn): U;
    ok(): Option<T>;
    orElse<F>(fn): Result<T, F>;
    unwrap(): T;
    unwrapErr(): E;
    unwrapOr(def): T;
}

Type Parameters

  • T extends NonUndefined
  • E extends NonUndefined

Properties

type: symbol

Represents the type of the Result: either Ok or Err. Useful for debugging and runtime checks.

Methods

  • Transforms the Result by applying a function that returns a Result to the contained Ok value, chaining multiple potentially failing operations.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: ((val) => Result<U, E>)

      A function that takes the current Result's Ok value and returns a new Result.

    Returns Result<U, E>

    The Result returned by the function if the original Result is Ok, otherwise the original Err.

    Examples

    const parse = (s: string) => {
    const parsed = parseInt(s);
    return isNaN(parsed) ? Err("NaN") : Ok(parsed);
    };
    const result = Ok("123").andThen(parse); // Ok(123)
    const noResult = Err("NaN").andThen(parse); // Err("NaN")
  • Converts the Result to an Option containing the error value, or None if Ok.

    Returns Option<E>

    An Option of the error value if Err, otherwise None.

    Examples

    const result = Ok(5).err(); // None
    const error = Err("error").err(); // Some("error")
  • Checks if the Result is an Err.

    Returns this is ErrResult<T, E>

    true if the Result is Err, otherwise false.

    Example

    console.log(Ok(5).isErr()); // false
    console.log(Err("error").isErr()); // true
  • Checks if the Result is an Ok.

    Returns this is OkResult<T, E>

    true if the Result is Ok, otherwise false.

    Example

    console.log(Ok(5).isOk()); // true
    console.log(Err("error").isOk()); // false
  • Maps a Result<Ok, Err> to Result<U, Err> by applying a function to a contained Ok value, leaving an Err value untouched.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: ((val) => U)

      A function to apply to the contained Ok value.

        • (val): U
        • Parameters

          • val: T

          Returns U

    Returns Result<U, E>

    A Result containing the transformed value if Ok, or the original Err.

    Examples

    const length = Ok("hello").map(s => s.length); // Ok(5)
    const error = Err("error").map(s => s.length); // Err("error")
  • Maps a Result<Ok, Err> to Result<Ok, F> by applying a function to a contained Err value, leaving an Ok value untouched.

    Type Parameters

    • F extends NonUndefined

    Parameters

    • fn: ((err) => F)

      A function to apply to the contained Err value.

        • (err): F
        • Parameters

          • err: E

          Returns F

    Returns Result<T, F>

    A Result containing the original Ok, or the transformed Err.

    Examples

    const ok = Ok("hello").mapErr(err => `Error: ${err}`); // Ok("hello")
    const error = Err("error").mapErr(err => `Error: ${err}`); // Err("Error: error")
  • Performs a match operation on the Result, allowing for branching logic based on its state.

    Type Parameters

    • U extends NonUndefined

    Parameters

    • fn: Match<T, E, U>

      An object with functions to handle each case: Ok or Err.

    Returns U

    The result of applying the appropriate function based on the Result's state.

    Examples

    const result = Ok(5).match({
    ok: (val) => `Success: ${val}`,
    err: (val) => `Error: ${val}`,
    });
    // result === "Success: 5"

    const error = Err("failure").match({
    ok: (val) => `Success: ${val}`,
    err: (val) => `Error: ${val}`,
    });
    // error === "Error: failure"
  • Converts the Result to an Option containing the success value, or None if Err.

    Returns Option<T>

    An Option of the success value if Ok, otherwise None.

    Examples

    const result = Ok(5).ok(); // Some(5)
    const error = Err("error").ok(); // None
  • Applies a function to the contained Err value if present, or returns the Ok value if present.

    Type Parameters

    • F extends NonUndefined

    Parameters

    • fn: ((err) => Result<T, F>)

      A function that takes the current Result's Err value and returns a new Result.

    Returns Result<T, F>

    The original Ok if present, or the Result of the function applied to the Err value.

    Examples

    const ok = Ok("hello").orElse(err => Err(`Error: ${err}`)); // Ok("hello")
    const error = Err("error").orElse(err => Ok(`Recovered from ${err}`)); // Ok("Recovered from error")
  • Unwraps a Result, yielding the contained success value if Ok, otherwise throws an error.

    Returns T

    The contained success value.

    Throws

    Error if the Result is Err.

    Examples

    console.log(Ok("value").unwrap()); // "value"
    console.log(Err("error").unwrap()); // throws Error
  • Unwraps a Result, yielding the contained error value if Err, otherwise throws an error.

    Returns E

    The contained error value.

    Throws

    Error if the Result is Ok.

    Examples

    console.log(Err("error").unwrapErr()); // "error"
    console.log(Ok("value").unwrapErr()); // throws Error
  • Returns the contained success value if Ok, otherwise returns the provided default value.

    Parameters

    • def: T

      The default value to return if the Result is Err.

    Returns T

    The contained success value if Ok, otherwise def.

    Examples

    const result = Ok("value").unwrapOr("default"); // "value"
    const error = Err("error").unwrapOr("default"); // "default"