std
    Preparing search index...

    Class Result<T, E>

    Result allows you to show to a consumer that a function might throw and force them to handle it.

    T Value type

    E Error type

    function functionThatMightFail(): Result<T, E>;
    
    const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");

    const result = functionThatMightFail();

    console.log(result.unwrap()); // "Hello, World!"

    Type Parameters

    • T
    • E
    Index

    Constructors

    Methods

    • Tries to return value if value is Err throws custom error message.

      Parameters

      • message: string

        Message to show when value is Err

      Returns T

      result.expect("Custom message");
      
      const functionThatMightFail = (): Result<string, string> => Ok("Hello!");

      const result = functionThatMightFail();

      console.log(result.expect("I failed!")); // "Hello!"
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      result.expect("I failed!"); // Error: I failed!
    • Tries to return error value if value is Ok throws custom error message

      Parameters

      • message: string

      Returns E

      result.expectErr("Custom message");
      
      const functionThatMightFail = (): Result<string, string> => Ok("Hello!");

      const result = functionThatMightFail();

      console.log(result.expectErr("I failed!")); // Error: I failed!
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      console.log(result.expectErr("I failed!")); // "oops!"
    • Returns true if result is Err

      Returns boolean

      result.isErr();
      
    • Returns true if result is Ok

      Returns boolean

      result.isOk();
      
    • Maps Result<T, E> to Result<A, E> using the passed mapping function

      Type Parameters

      • A

      Parameters

      • fn: (val: T) => A

        Mapping function

      Returns Result<A, E>

      result.map((val) => val.length);
      
      const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");

      const result = functionThatMightFail();

      const hello = result.map((val) => val.slice(0, 5));

      console.log(hello.unwrap()); // "Hello"
    • Maps Result<T, E> to Result<T, A> using the passed mapping function

      Type Parameters

      • A

      Parameters

      • fn: (err: E) => A

        Mapping function

      Returns Result<T, A>

      result.mapErr((err) => getCodeMsg(err));
      
      const functionThatMightFail = (): Result<string, string> => Err(10);

      const result = functionThatMightFail();

      const message = result.mapErr(() => "Error");

      console.log(message); // "Error"
    • In the Err case returns the mapped value using the function else returns defaultVal

      Type Parameters

      • A

      Parameters

      • defaultVal: A

        Value to be returned when Ok

      • fn: (err: E) => A

        Mapping function to map in case of Err

      Returns A

      result.mapErrOr("Should've been error", (err) => getCodeMsg(err));
      
      const functionThatMightFail = (): Result<string, string> => Ok("foo");

      const result = functionThatMightFail();

      const message = result.mapErrOr("Should've been error", () => "Error");

      console.log(message); // "Should've been error"
      const functionThatMightFail = (): Result<string, string> => Err(10);

      const result = functionThatMightFail();

      const message = result.mapErrOr("Should've been error", () => "Error");

      console.log(message); // "Error"
    • In the Err case returns the mapped value using the function else returns value of def

      Type Parameters

      • A

      Parameters

      • def: (val: T) => A

        Mapping function called when Ok

      • fn: (err: E) => A

        Mapping function called when Err

      Returns A

      result.mapErrOrElse(() => "Value", (_) => "Error!");
      
      const functionThatMightFail = (): Result<string, string> => Ok("foo");

      const result = functionThatMightFail();

      const length = result.mapErrOrElse(() => 1, (val) => val.length);

      console.log(length); // 1
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      const length = result.mapOr(() => 1, (val) => val.length);

      console.log(length); // 4
    • In the Ok case returns the mapped value using the function else returns defaultVal

      Type Parameters

      • A

      Parameters

      • defaultVal: A

        Value to be returned when Err

      • fn: (val: T) => A

        Mapping function to map in case of Ok

      Returns A

      result.mapOr(1, (val) => val.length);
      
      const functionThatMightFail = (): Result<string, string> => Ok("foo");

      const result = functionThatMightFail();

      const length = result.mapOr(1, (val) => val.length);

      console.log(length); // 3
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      const length = result.mapOr(1, (val) => val.length);

      console.log(length); // 1
    • In the Ok case returns the mapped value using fn else returns value of def

      Type Parameters

      • A

      Parameters

      • def: (err: E) => A

        Mapping function called when Err

      • fn: (val: T) => A

        Mapping function called when Ok

      Returns A

      result.mapOrElse(() => 1, (val) => val.length);
      
      const functionThatMightFail = (): Result<string, string> => Ok("foo");

      const result = functionThatMightFail();

      const length = result.mapOrElse(() => 1, (val) => val.length);

      console.log(length); // 3
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      const length = result.mapOr(() => 1, (val) => val.length);

      console.log(length); // 1
    • Allows you to run callbacks based on the result.

      Type Parameters

      • A
      • B = A

      Parameters

      • success: (val: T) => A

        callback to be run when result is success

      • failure: (err: E) => B

        callback to be run when result is failure

      Returns A | B

      result.match(
      (val) => val,
      () => {
      throw new Error('oops!')
      }
      );
      const functionThatMightFail = (): Result<string, string> => Ok("Hello, World!");

      const result = functionThatMightFail();

      const val = result.match(
      (val) => val,
      () => {
      throw new Error('oops!')
      }
      );

      console.log(val); // "Hello, World!"
    • Tries to return value if value is Err throws generic error message.

      Returns T

      result.unwrap();
      
      const functionThatMightFail = (): Result<string, string> => Ok("Hello!");

      const result = functionThatMightFail();

      console.log(result.unwrap()); // "Hello!"
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      result.unwrap(); // Error: Attempted to call `.unwrap()` on a non `Ok` value.
    • Tries to return err if value is Ok throws generic error message.

      Returns E

      result.unwrapErr();
      
      const functionThatMightFail = (): Result<string, string> => Ok("Hello!");

      const result = functionThatMightFail();

      result.unwrapErr(); // Error: Attempted to call `.unwrapErr()` on a non `Err` value.
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      console.log(result.unwrapErr()); // "oops!"
    • Tries to unwrap the error if vale is Ok returns defaultVal

      Parameters

      • defaultVal: E

      Returns E

      result.unwrapErrOr("Error");
      
      const functionThatMightFail = (): Result<string, string> => Ok("Hello!");

      const result = functionThatMightFail();

      console.log(result.unwrapErrOr("Yellow!")); // "Yellow!"
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      console.log(result.unwrapErrOr("Yellow!")); // "oops!"
    • Tries to return the error if value is Ok calls fn

      Parameters

      • fn: (val: T) => E

        Function called if Ok

        result.unwrapErrOrElse(() => "Error!");
        
        const functionThatMightFail = (): Result<string, string> => Ok("Hello!");

        const result = functionThatMightFail();

        console.log(result.unwrapErrOrElse(() => "oops!")); // "oops!"
        const functionThatMightFail = (): Result<string, string> => Err("oops!");

        const result = functionThatMightFail();

        console.log(result.unwrapErrOrElse(() => "Hello!")); // "oops!"

      Returns E

    • Tries to unwrap the value if value is Err returns defaultVal

      Parameters

      • defaultVal: T

        Value to be returned if Err

      Returns T

      result.unwrapOr(7);
      
      const functionThatMightFail = (): Result<string, string> => Ok("Hello!");

      const result = functionThatMightFail();

      console.log(result.unwrapOr("Yellow!")); // "Hello!"
      const functionThatMightFail = (): Result<string, string> => Err("oops!");

      const result = functionThatMightFail();

      console.log(result.unwrapOr("Yellow!")); // "Yellow!"
    • Tries to return the value if value is Err calls fn

      Parameters

      • fn: (err: E) => T

        Function called if Err

        result.unwrapOrElse(() => "Hello!");
        
        const functionThatMightFail = (): Result<string, string> => Ok("Hello!");

        const result = functionThatMightFail();

        console.log(result.unwrapOrElse(() => "oops!")); // "Hello!"
        const functionThatMightFail = (): Result<string, string> => Err("oops!");

        const result = functionThatMightFail();

        console.log(result.unwrapOrElse(() => "Hello!")); // "Hello!"

      Returns T