A implementation of Option, Result and Future types in TypeScript. If you know Rust, you will feel right at home.
import { Option } from '@frank-mayer/opsult/Option';
const a: Option<number> = some(1);
const b: Option<number> = none();
const c: Option<number> = some(2);
a.andThen((x: number) => c.map((y: number) => x + y)); // some(3)
b.andThen((x: number) => c.map((y: number) => x + y)); // none()
import { Result } from '@frank-mayer/opsult/Result';
const a: Result<number, string> = ok(1);
const b: Result<number, string> = err('error');
const c: Result<number, string> = ok(2);
a.andThen((x: number) => c.map((y: number) => x + y)); // ok(3)
b.andThen((x: number) => c.map((y: number) => x + y)); // err('error')
import { Future } from '@frank-mayer/opsult/Future';
const fut: Future<number, string> = new Future<number, string>((ok, err) => {
setTimeout(() => err("timeout"), 1000);
complexAsyncOperation((x: number) => {
ok(x);
});
})
const res: Result<number, string> = await fut;
res.match({
ok: (x: number) => console.log(x),
err: (e: string) => console.error(e)
});
Read the docs to learn on how to use it.
npm i @frank-mayer/opsult