An open API service indexing awesome lists of open source software.

https://github.com/tsukinoko-kun/opsult

A simple implementation of Option, Result and Future types in TypeScript
https://github.com/tsukinoko-kun/opsult

future option result result-type rust type-library typescript

Last synced: 14 days ago
JSON representation

A simple implementation of Option, Result and Future types in TypeScript

Awesome Lists containing this project

README

        



opsult



Types included

License: MIT
Test
Lint

## Basic overfiew

A implementation of Option, Result and Future types in TypeScript. If you know Rust, you will feel right at home.

### Option

```TypeScript
import { Option } from '@frank-mayer/opsult/Option';

const a: Option = some(1);
const b: Option = none();
const c: Option = 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()
```

### Result

```TypeScript
import { Result } from '@frank-mayer/opsult/Result';

const a: Result = ok(1);
const b: Result = err('error');
const c: Result = 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')
```

### Future

```TypeScript
import { Future } from '@frank-mayer/opsult/Future';

const fut: Future = new Future((ok, err) => {
setTimeout(() => err("timeout"), 1000);

complexAsyncOperation((x: number) => {
ok(x);
});
})

const res: Result = 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.](https://Frank-Mayer.github.io/opsult)

## Installation

```bash
npm i @frank-mayer/opsult
```