Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Result<T, E>

Result is a type that represents either success (Ok) or failure (Err).

See the result module documentation for details.

Type parameters

  • T

  • E

Hierarchy

  • Result

Implements

Index

Constructors

constructor

  • new Result(value: T, error: E, hasOk: boolean): Result

Properties

Protected error

error: E

Protected hasOk

hasOk: boolean

Protected value

value: T

Accessors

err

  • Converts from Result<T, E> to Option.

    Converts result into an Option, consuming result, and discarding the success value, if any.

    Examples:

    Basic usage:

    const x: Result<number, string> = Ok(2);
    assert.deepEqual(x.err, None());
    
    const x: Result<number, string> = Err('Nothing here');
    assert.deepEqual(x.err, Some('Nothing here'));
    

    Returns Option<E>

isErr

  • get isErr(): boolean
  • Returns true if the result is Err.

    Examples

    Basic usage:

    const x: Result<number, string> = Ok(-3);
    assert.equal(x.isErr, false);
    
    const x: Result<number, string> = Err('Some error message');
    assert.equal(x.isErr, true);
    

    Returns boolean

isOk

  • get isOk(): boolean
  • Returns true if the result is Ok.

    Examples

    Basic usage:

    const x: Result<number, string> = Ok(-3);
    assert.equal(x.isOk, true);
    
    const x: Result<number, string> = Err('Some error message');
    assert.equal(x.isOk, false);
    

    Returns boolean

ok

  • Converts from Result<T, E> to Option.

    Converts result into an Option, consuming result, and discarding the error, if any.

    Examples:

    Basic usage:

    const x: Result<number, string> = Ok(2);
    assert.deepEqual(x.ok, Some(2));
    
    const x: Result<number, string> = Err('Nothing here');
    assert.deepEqual(x.ok, None());
    

    Returns Option<T>

Methods

and

  • Returns res if the result is Ok, otherwise returns the Err value of self.

    Examples

    Basic usage:

    const x1: Result<number, string> = Ok(2);
    const y1: Result<string, string> = Err('late error');
    assert_eq!(x1.and(y1), Err('late error'));
    
    const x2: Result<number, string> = Err('early error');
    const y2: Result<string, string> = Ok('foo');
    assert.deepEqual(x2.and(y2), Err('early error'));
    
    const x3: Result<number, string> = Err('not a 2');
    const y3: Result<string, string> = Err('late error');
    assert.deepEqual(x3.and(y3), Err('not a 2'));
    
    const x4: Result<number, string> = Ok(2);
    const y4: Result<string, string> = Ok('different result type');
    assert.deepEqual(x4.and(y4), Ok('different result type'));
    

    Type parameters

    • U

    Parameters

    • res: Result<U, E>

      another result

    Returns Result<U, E>

andThen

  • andThen<U>(cb: function): Result<U, E>
  • Calls cb if the result is Ok, otherwise returns the Err value of self.

    This function can be used for control flow based on Result values.

    Examples

    Basic usage:

    type Fn = (x: number) => Result<number, number>
    const sq: Fn = x => Ok(x * x);
    const err: Fn = x => Err(x);
    
    assert.deepEqual(Ok(2).andThen(sq).andThen(sq), Ok(16));
    assert.deepEqual(Ok(2).andThen(sq).andThen(err), Err(4));
    assert.deepEqual(Ok(2).andThen(err).andThen(sq), Err(2));
    assert.deepEqual(Err(3).andThen(sq).andThen(sq), Err(3));
    

    Type parameters

    • U

    Parameters

    • cb: function

      callback to map wrapped value

        • Parameters

          • value: T

          Returns Result<U, E>

    Returns Result<U, E>

expect

  • expect(msg: string): T
  • Unwraps a result, yielding the content of an Ok.

    Throws

    Throws an error if the value is an Err, with a error message including the passed message, and the content of the Err.

    Examples

    Basic usage:

    const x: Result<number, string> = Err('emergency failure');
    x.expect('Testing expect'); // panics with `Testing expect: emergency failure`
    

    Parameters

    • msg: string

      additional custom error message

    Returns T

map

  • map<U>(cb: function): Result<U, E>
  • 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.

    Examples

    Print the numbers on each line of a string multiplied by two.

    let line = '1\n2\n3\n4\n';
    
    const parse = (num: string): Result<number, string> => {
      const parsed = parseInt(num, 10);
      return parsed
        ? Ok(parsed)
        : Err('error');
    };
    
    for (const num of line.split('\n')) {
      parse(num).map(i => i * 2).match({
        Ok: v => console.log(v),
        Err: () => {}
      });
    }
    

    Type parameters

    • U

    Parameters

    • cb: function

      callback to map wrapped value

        • (value: T): U
        • Parameters

          • value: T

          Returns U

    Returns Result<U, E>

mapErr

  • mapErr<F>(cb: function): Result<T, F>
  • 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.

    Examples

    Basic usage:

    const stringify = (x: number) => `error code: ${x}`;
    
    const x: Result<number, number> = Ok(2);
    assert.deepEqual(x.mapErr(stringify), Ok(2));
    
    const y: Result<number, number> = Err(13);
    assert.deepEqual(y.mapErr(stringify), Err('error code: 13'));
    

    Type parameters

    • F

    Parameters

    • cb: function

      callback to map error value

        • (error: E): F
        • Parameters

          • error: E

          Returns F

    Returns Result<T, F>

match

  • Matches result to the received matcher and executes appropriate branch.

    Examples

    Basic usage:

    const x1 = Ok(10);
    
    const y1 = x1.match({
      Ok: v => v + 10,
      Err: () => 100
    });
    
    assert.equal(y1, 20);
    
    const x2 = Err('error');
    
    const y2 = x2.match({
      Ok: v => v + 10,
      Err: () => 100
    });
    
    assert.equal(y2, 100);
    
    const x3 = Err('error');
    
    x3.match({
      Ok: v => console.log(v),
      Err: e => console.log(e + 's') // prints "errors"
    });
    

    Type parameters

    • U

    Parameters

    Returns U

or

  • Returns res if the result is Err, otherwise returns the Ok value of self.

    Examples

    Basic usage:

    const x1: Result<number, string> = Ok(2);
    const y1: Result<number, string> = Err('late error');
    assert.deepEqual(x1.or(y1), Ok(2));
    
    const x2: Result<number, string> = Err('early error');
    const y2: Result<number, string> = Ok(2);
    assert.deepEqual(x2.or(y2), Ok(2));
    
    const x3: Result<number, string> = Err('not a 2');
    const y3: Result<number, string> = Err('late error');
    assert.deepEqual(x3.or(y3), Err('late error'));
    
    const x4: Result<number, string> = Ok(2);
    const y4: Result<number, string> = Ok(100);
    assert.deepEqual(x4.or(y4), Ok(2));
    

    Type parameters

    • F

    Parameters

    • res: Result<T, F>

      default result

    Returns Result<T, F>

orElse

  • orElse<F>(cb: function): Result<T, F>
  • Calls cb if the result is Err, otherwise returns the Ok value of self.

    This function can be used for control flow based on result values.

    Examples

    Basic usage:

    type Fn = (x: number) => Result<number, number>
    const sq: Fn = x => Ok(x * x);
    const err: Fn = x => Err(x);
    
    assert.deepEqual(Ok(2).orElse(sq).orElse(sq), Ok(2));
    assert.deepEqual(Ok(2).orElse(err).orElse(sq), Ok(2));
    assert.deepEqual(Err(3).orElse(sq).orElse(err), Ok(9));
    assert.deepEqual(Err(3).orElse(err).orElse(err), Err(3));
    

    Type parameters

    • F

    Parameters

    • cb: function

      callback to calculate default result

        • Parameters

          • error: E

          Returns Result<T, F>

    Returns Result<T, F>

unwrap

  • unwrap(): T
  • Unwraps a result, yielding the content of an Ok.

    Throws

    Throws an error if the value is an Err, with a error message provided by the Err's value.

    Examples

    Basic usage:

    const x: Result<number, string> = Ok(2);
    assert.equal(x.unwrap(), 2);
    
    const x: Result<number, string> = Err('emergency failure');
    x.unwrap(); // fails with `emergency failure`
    

    Returns T

unwrapErr

  • unwrapErr(): E
  • Unwraps a result, yielding the content of an Err.

    Throws

    Throws an error if the value is an Ok, with a custom error message provided by the Ok's value.

    Examples

    Basic usage:

    const x: Result<number, string> = Ok(2);
    x.unwrapErr(); // panics with `2`
    
    const x: Result<number, string> = Err('emergency failure');
    assert.equal(x.unwrapErr(), 'emergency failure');
    

    Returns E

unwrapOr

  • unwrapOr(optb: T): T
  • Unwraps a result, yielding the content of an Ok. Else, it returns optb.

    Examples

    Basic usage:

    const optb = 2;
    const x: Result<number, string> = Ok(9);
    assert.equal(x.unwrapOr(optb), 9);
    
    const y: Result<number, string> = Err("error");
    assert.equal(y.unwrapOr(optb), optb);
    

    Parameters

    • optb: T

      default

    Returns T

unwrapOrElse

  • unwrapOrElse(cb: function): T
  • Unwraps a result, yielding the content of an Ok. If the value is an Err then it calls cb with its value.

    Examples

    Basic usage:

    const count: (x: string) => number = x => x.length;
    
    assert.equal(Ok(2).unwrapOrElse(count), 2);
    assert.equal(Err("foo").unwrapOrElse(count), 3);
    

    Parameters

    • cb: function

      callback to calculate default value

        • (error: E): T
        • Parameters

          • error: E

          Returns T

    Returns T

Generated using TypeDoc