@binarymuse/ts-stdlib
    Preparing search index...

    Interface ResultMethods<T, E>

    All the methods available on Result<T, E>.

    interface ResultMethods<T, E> {
        and: <U>(other: Result<U, E>) => Result<U, E>;
        andThen: <U>(fn: (value: T) => Result<U, E>) => Result<U, E>;
        equals: (other: Result<T, E>) => boolean;
        err: () => Option<E>;
        expect: (msg: string) => T;
        expectErr: (msg: string) => E;
        flatten: () => Result<T, E>;
        inspect: (fn: (value: T) => void) => Result<T, E>;
        inspectErr: (fn: (error: E) => void) => Result<T, E>;
        isErr: () => boolean;
        isErrAnd: (fn: (error: E) => boolean) => boolean;
        isOk: () => boolean;
        isOkAnd: (fn: (value: T) => boolean) => boolean;
        map: <U>(fn: (value: T) => U) => Result<U, E>;
        mapErr: <U>(fn: (error: E) => U) => Result<T, U>;
        mapOr: <U>(defaultValue: U, fn: (value: T) => U) => Result<U, E>;
        mapOrElse: <U>(defaultFn: () => U, mapFn: (value: T) => U) => Result<U, E>;
        ok: () => Option<T>;
        or: <U>(other: Result<U, E>) => Result<U, E>;
        orElse: <U>(fn: () => Result<U, E>) => Result<U, E>;
        strictEquals: (other: Result<T, E>) => boolean;
        unwrap: () => T;
        unwrapErr: () => E;
        unwrapOr: <U>(defaultValue: T | U) => T | U;
        unwrapOrElse: <U>(fn: () => T | U) => T | U;
    }

    Type Parameters

    • T

      The type of the success value

    • E

      The type of the error value

    Index

    Properties

    Equality Checks

    Inspect Methods

    Query Methods

    Transform Methods

    Unwrap Methods

    Properties

    equals: (other: Result<T, E>) => boolean

    Returns true if both results are Ok and their success values are equal using the JavaScript == operator, or if both results are Err and their error values are equal using the JavaScript == operator. If the inner values are Results or Options, they will be compared using their own equals method.

    Ok(1).equals(Ok(1)); // true
    Ok(Ok(1)).equals(Ok(Ok(1))); // true
    Ok(Some(1)).equals(Ok(Some(1))); // true
    Ok(Err(1)).equals(Ok(Err(1))); // true
    Err(1).equals(Err(1)); // true
    Err(1).equals(Ok(1)); // false

    @param other - The result to compare against.
    @returns {boolean} Whether the results are equal.
    @group Equality

    Equality Checks

    strictEquals: (other: Result<T, E>) => boolean

    Returns true if both results are Ok and their success values are equal using the JavaScript === operator, or if both results are Err and their error values are equal using the JavaScript === operator. If the inner values are Results or Options, they will be compared using their own strictEquals method.

    Type declaration

      • (other: Result<T, E>): boolean
      • Parameters

        • other: Result<T, E>

          The result to compare against.

        Returns boolean

        Whether the results are strictly equal.

    const obj = {}

    Ok(obj).strictEquals(Ok(obj)); // true
    Ok(obj).strictEquals(Ok({})); // false
    Err(obj).strictEquals(Err(obj)); // true
    Err(obj).strictEquals(Err({})); // false
    Ok(Some(obj)).strictEquals(Ok(Some(obj))); // true

    Inspect Methods

    inspect: (fn: (value: T) => void) => Result<T, E>

    Calls fn with the success value if the result is Ok, otherwise does nothing. Returns the original result.

    Type declaration

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

        • fn: (value: T) => void

          A function that takes the success value.

        Returns Result<T, E>

        The original result.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.inspect(value => console.log(value)); // logs 1, returns result
    const result4 = result2.inspect(value => console.log(value)); // Does nothing, returns result2
    inspectErr: (fn: (error: E) => void) => Result<T, E>

    Calls fn with the error value if the result is Err, otherwise does nothing. Returns the original result.

    Type declaration

      • (fn: (error: E) => void): Result<T, E>
      • Parameters

        • fn: (error: E) => void

          A function that takes the error value.

        Returns Result<T, E>

        The original result.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.inspectErr(error => console.log(error)); // Does nothing, returns result
    const result4 = result2.inspectErr(error => console.log(error)); // logs "error", returns result2

    Query Methods

    isErr: () => boolean

    Returns true if the result is Err.

    Type declaration

      • (): boolean
      • Returns boolean

        true if the result is Err, false otherwise.

    const result = Err("error");
    const isErr = result.isErr(); // true

    const result2 = Ok(1);
    const isErr2 = result2.isErr(); // false
    isErrAnd: (fn: (error: E) => boolean) => boolean

    Returns true if the result is Err and the given function returns true.

    Type declaration

      • (fn: (error: E) => boolean): boolean
      • Parameters

        • fn: (error: E) => boolean

          A function that takes the error value and returns a boolean.

        Returns boolean

        true if the result is Err and the given function returns true, false otherwise.

    const result = Err("error");
    const isErrAnd = result.isErrAnd(error => error === "error"); // true

    const result2 = Ok(1);
    const isErrAnd2 = result2.isErrAnd(error => error === "error"); // false
    isOk: () => boolean

    Returns true if the result is Ok.

    Type declaration

      • (): boolean
      • Returns boolean

        true if the result is Ok, false otherwise.

    const result = Ok(1);
    const isOk = result.isOk(); // true

    const result2 = Err("error");
    const isOk2 = result2.isOk(); // false
    isOkAnd: (fn: (value: T) => boolean) => boolean

    Returns true if the result is Ok and the given function returns true.

    Type declaration

      • (fn: (value: T) => boolean): boolean
      • Parameters

        • fn: (value: T) => boolean

          A function that takes the success value and returns a boolean.

        Returns boolean

        true if the result is Ok and the given function returns true, false otherwise.

    const result = Ok(1);
    const isOkAnd = result.isOkAnd(value => value > 0); // true

    const result2 = Err("error");
    const isOkAnd2 = result2.isOkAnd(value => value > 0); // false

    Transform Methods

    and: <U>(other: Result<U, E>) => Result<U, E>

    Returns other if the result is Ok, otherwise returns the current error.

    Type declaration

      • <U>(other: Result<U, E>): Result<U, E>
      • Type Parameters

        • U

          The type of the success value in the new result.

        Parameters

        • other: Result<U, E>

          The result to return if this result is Ok.

        Returns Result<U, E>

        Either the provided result or an error result.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.and(result2); // Ok(1)
    const result4 = result2.and(result); // Err("error")
    andThen: <U>(fn: (value: T) => Result<U, E>) => Result<U, E>

    Returns None if the result is Err, otherwise calls fn with the success value and returns the result.

    Type declaration

      • <U>(fn: (value: T) => Result<U, E>): Result<U, E>
      • Type Parameters

        • U

          The type of the success value in the new result.

        Parameters

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

          A function that takes the success value and returns a new result.

        Returns Result<U, E>

        The result of fn or an error result.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.andThen(value => Ok(value * 2)); // Ok(2)
    const result4 = result2.andThen(value => Ok(value * 2)); // Err("error")
    err: () => Option<E>

    Converts the Result<T, E> into an Option<E>, mapping Ok(_) to None and Err(e) to Some(e).

    Type declaration

    const result = Ok(1);
    const option = result.err(); // None
    const result2 = Err("error");
    const option2 = result2.err(); // Some("error")
    flatten: () => Result<T, E>

    Converts from Result<Result<T, E>, E> to Result<T, E>. Only one level of nesting is removed.

    Type declaration

    const result = Ok(Ok(1));
    const result2 = Err(Err("error"));
    const result3 = result.flatten(); // Ok(1)
    const result4 = result2.flatten(); // Err("error")
    map: <U>(fn: (value: T) => U) => Result<U, E>

    Maps a Result<T, E> to Result<U, E> by applying the function to the success value.

    Type declaration

      • <U>(fn: (value: T) => U): Result<U, E>
      • Type Parameters

        • U

          The type of the new success value.

        Parameters

        • fn: (value: T) => U

          A function that takes the success value and returns a new value.

        Returns Result<U, E>

        A new result with the transformed success value.

    const result = Ok(1);
    const result2 = result.map(value => value * 2); // Ok(2)
    const result3 = Err("error");
    const result4 = result3.map(value => value * 2); // Err("error")
    mapErr: <U>(fn: (error: E) => U) => Result<T, U>

    Maps a Result<T, E> to Result<T, U> by applying the function to the error value.

    Type declaration

      • <U>(fn: (error: E) => U): Result<T, U>
      • Type Parameters

        • U

          The type of the new error value.

        Parameters

        • fn: (error: E) => U

          A function that takes the error value and returns a new error value.

        Returns Result<T, U>

        A new result with the transformed error value.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.mapErr(error => error.toUpperCase()); // Ok(1)
    const result4 = result2.mapErr(error => error.toUpperCase()); // Err("ERROR")
    mapOr: <U>(defaultValue: U, fn: (value: T) => U) => Result<U, E>

    Returns a result wrapping the provided defaultValue if the result is Err, or calls fn with the success value and returns a new result wrapping its return value.

    Type declaration

      • <U>(defaultValue: U, fn: (value: T) => U): Result<U, E>
      • Type Parameters

        • U

          The type of the new success value.

        Parameters

        • defaultValue: U

          The value to use if the result is Err.

        • fn: (value: T) => U

          A function that takes the success value and returns a new value.

        Returns Result<U, E>

        A new result containing either the default or transformed value.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.mapOr(0, value => value * 2); // Ok(2)
    const result4 = result2.mapOr(0, value => value * 2); // Ok(0)
    mapOrElse: <U>(defaultFn: () => U, mapFn: (value: T) => U) => Result<U, E>

    Returns a result wrapping the return value of defaultFn if the result is Err, or calls mapFn with the success value and returns a new result wrapping its return value. This function is useful for providing a default result that is expensive to compute or has side effects.

    Type declaration

      • <U>(defaultFn: () => U, mapFn: (value: T) => U): Result<U, E>
      • Type Parameters

        • U

          The type of the new success value.

        Parameters

        • defaultFn: () => U

          A function that returns the default value if the result is Err.

        • mapFn: (value: T) => U

          A function that takes the success value and returns a new value.

        Returns Result<U, E>

        A new result containing either the default or transformed value.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.mapOrElse(() => 0, value => value * 2); // Ok(2)
    const result4 = result2.mapOrElse(() => 0, value => value * 2); // Ok(0)
    ok: () => Option<T>

    Converts the Result<T, E> into an Option<T>, mapping Ok(v) to Some(v) and Err(_) to None.

    Type declaration

      • (): Option<T>
      • Returns Option<T>

        An option containing the success value.

    const result = Ok(1);
    const option = result.ok(); // Some(1)
    const result2 = Err("error");
    const option2 = result2.ok(); // None
    or: <U>(other: Result<U, E>) => Result<U, E>

    Returns the current result if it is Ok, otherwise returns other.

    Type declaration

      • <U>(other: Result<U, E>): Result<U, E>
      • Type Parameters

        • U

          The type of the success value in the other result.

        Parameters

        • other: Result<U, E>

          The result to return if this result is Err.

        Returns Result<U, E>

        Either this result or the provided result.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.or(result2); // Ok(1)
    const result4 = result2.or(result); // Ok(1)
    orElse: <U>(fn: () => Result<U, E>) => Result<U, E>

    Returns the current result if it is Ok, otherwise calls fn and returns the result. This function is useful for providing a default result that is expensive to compute or has side effects.

    Type declaration

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

        • U

          The type of the success value in the new result.

        Parameters

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

          A function that returns a new result.

        Returns Result<U, E>

        Either this result or the result of fn.

    const result = Ok(1);
    const result2 = Err("error");
    const result3 = result.orElse(() => result2); // Ok(1)
    const result4 = result2.orElse(() => result); // Ok(1)

    Unwrap Methods

    expect: (msg: string) => T

    Returns the success value. If the result is Err, this will throw an error with the provided message.

    Type declaration

      • (msg: string): T
      • Parameters

        • msg: string

          The message to throw if the result is Err.

        Returns T

        The success value.

    const result = Ok(1);
    const value = result.expect("Expected a value"); // 1

    const result2 = Err("error");
    const value2 = result2.expect("Expected a value"); // throws Error
    expectErr: (msg: string) => E

    Returns the error value. If the result is Ok, this will throw an error with the provided message.

    Type declaration

      • (msg: string): E
      • Parameters

        • msg: string

          The message to throw if the result is Ok.

        Returns E

        The error value.

    const result = Ok(1);
    const value = result.expectErr("Expected an error"); // throws Error

    const result2 = Err("error");
    const value2 = result2.expectErr("Expected an error"); // "error"
    unwrap: () => T

    Returns the success value. If the result is Err, this will throw an error.

    Type declaration

      • (): T
      • Returns T

        The success value.

    const result = Ok(1);
    const value = result.unwrap(); // 1

    const result2 = Err("error");
    const value2 = result2.unwrap(); // throws Error
    unwrapErr: () => E

    Returns the error value. If the result is Ok, this will throw an error.

    Type declaration

      • (): E
      • Returns E

        The error value.

    const result = Err("error");
    const error = result.unwrapErr(); // "error"

    const result2 = Ok(1);
    const error2 = result2.unwrapErr(); // throws Error
    unwrapOr: <U>(defaultValue: T | U) => T | U

    Returns the success value. If the result is Err, this will return the provided default value.

    Type declaration

      • <U>(defaultValue: T | U): T | U
      • Type Parameters

        • U

        Parameters

        • defaultValue: T | U

          The value to return if the result is Err.

        Returns T | U

        The success value or the default value.

    const result = Ok(1);
    const value = result.unwrapOr(0); // 1

    const result2 = Err("error");
    const value2 = result2.unwrapOr(0); // 0
    unwrapOrElse: <U>(fn: () => T | U) => T | U

    Returns the success value. If the result is Err, this will call the provided function and return its result. This function is useful for providing a default value that is expensive to compute or has side effects.

    Type declaration

      • <U>(fn: () => T | U): T | U
      • Type Parameters

        • U

        Parameters

        • fn: () => T | U

          A function that returns the default value if the result is Err.

        Returns T | U

        The success value or the result of the function.

    const result = Ok(1);
    const value = result.unwrapOrElse(() => 0); // 1

    const result2 = Err("error");
    const value2 = result2.unwrapOrElse(() => 0); // 0