0.3.0 • Published 2 years ago

@yajamon/result.ts v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

result.ts

Type definitions and simple functions inspired by Rust's Result.

Install

npm i -D @yajamon/result.ts

Types

type ResultOk<T> = {
  isError: false;
  value: T;
};

type ResultErr<E> = {
  isError: true;
  error: E;
};

export type Result<T, E> = ResultOk<T> | ResultErr<E>;

Usage

import { Result, ok, err } from "@yajamon/result.ts"

class Foo {
  static create(input:string): Result<Foo, Error> {
    if (validation(input)) {
      const foo = new Foo();
      // ...
      return ok(foo);
    } else {
      return err(new Error("error message"));
    }
  }

  // ...
}

function bar(input:string) {
  let foo = Foo.create(input);
  if (foo.isError === true) {
    // foo is ResultErr<Error>
    console.log(foo.error);
    return;
  }
  // foo is ResultOk<Foo>
  console.log(foo.value);
}
0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago