Skip to main content
Module

x/rusty_result/mod.ts>Result

Typescript implementation of Rust's Result
Latest
class Result
import { Result } from "https://deno.land/x/rusty_result@0.1.1/mod.ts";

Typescript implementation of Rust Result type.

// Create an ok Result
const myOkResult = Result.ok(1);
// Create an error Result
const myErrorResult = Result.error(2);
// Create a Result with an explicit ok and error type
const r = Result.error<number, number>(3);

// Create a function that takes Results
function myResultFunction(r: Result<number, number>): void {
  // Perform operation on ok value and get new Result
  const r2 = r.map((x) => x + 10);
  // Perform operation on ok value and on error value and get new Result
  const r3 = r.map((x) => x + 10).mapError((x) => x - 10);

  // Do action if Result is ok
  if (r3.isOk()) {
    console.log(`r3 is ok with value of: ${r3.unwrap()}`);
  }
  // Do action if Result is error
  if (r3.isError()) {
    console.log(`r3 is error with value of: ${r3.unwrapError()}`);
  }
}

// Use Result with a function
myResultFunction(r); // prints: r3 is error with value of: -7

Constructors

new
Result(
isOk: boolean,
ok?: Ok,
error?: Error,
)

AVOID USING THIS. Please use .ok() or .error() static class methods

Properties

private
readonly
isOkInner: boolean
private
readonly
value: Ok | Error

Methods

and<O>(res: Result<O, Error>): Result<O, Error>

Returns res if the result is Ok, otherwise returns itself which will be an Error.

andThen<O>(f: (x: Ok) => Result<O, Error>): Result<O, Error>

Calls f if the result is Ok, otherwise returns the Error value of itself.

error(): Error | undefined

Returns the inner Error value or undefined if value is Ok

isError(): boolean

Checks if Result is error

isOk(): boolean

Checks if Result is ok

map<O>(f: (ok: Ok) => O): Result<O, Error>

Applies function to result if it is ok and returns new result

Maps Error type or throws an Error if result is not ok

Maps Ok type or throws an Error if result is not error

mapError<E>(f: (error: Error) => E): Result<Ok, E>

Applies function to result if it is an error and returns new result

ok(): Ok | undefined

Returns the inner Ok value or undefined if value is Error

or<E>(res: Result<Ok, E>): Result<Ok, E>

Returns res if the result is Error, otherwise returns itself which will be an Ok.

orElse<E>(f: (x: Error) => Result<Ok, E>): Result<Ok, E>

Calls f if the result is Error, otherwise returns the Ok value of itself.

Get "ok" value or throw error. Note in the "empty case" that type Ok is void then undefined is returned.

Get "error" value or throw error. Note in the "empty case" that type Error is void then undefined is returned.

Get "ok" value or return x if this is an Error

unwrapOrElse(f: (x: Error) => Ok): Ok

Get "ok" value or return f(Error) if this is an Error

Static Methods

error<Ok, Error>(error: Error): Result<Ok, Error>

Create "error" value representing failure with error result. This can be used to create an error Result with a value or without one.

const myStringResult = Result.error('bar')
const myEmptyResult = Result.error()
error<Ok>(): Result<Ok, void>
ok<Ok, Error>(ok: Ok): Result<Ok, Error>

Create "ok" value representing successful result. This can be used to create an ok Result with a value or without one.

const myStringResult = Result.ok('foo')
const myEmptyResult = Result.ok()
ok<Error>(): Result<void, Error>