Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/bodil/opt

Option types for TypeScript with real gradual typing
https://github.com/bodil/opt

Last synced: 2 months ago
JSON representation

Option types for TypeScript with real gradual typing

Awesome Lists containing this project

README

        

# opt

Option types for TypeScript with real gradual typing.

## API Docs

Find the API docs here:

* [github.bodil.lol/opt](https://github.bodil.lol/opt)

## Rationale

There are several million implementations of option types on NPM. Many of them
are implemented in plain JavaScript with no types. Nearly all of the remainder
don't have very good type inference: their `isSome()` function just returns a
boolean rather than a useful `is` assertion, or they exclusively use some other
mechanism, like callbacks, to decide the type of the wrapped value. The very few
cases remaining implement the option type as a simple object manipulated by
external functions, rather than an object with attached methods, making them a
little too cumbersome to work with.

This package gives you both, as well as a complete and fully documented API
inspired by Rust's `Option` and `Result` types.

## Example

[`Result`](http://github.bodil.lol/opt/types/Result.html) in Node:

```typescript
import { Ok, Result } from "@bodil/opt";

const result: Result = Ok(new Date());

// result.value is Date | Error here.

if (result.isOk()) {
// the type checker now knows that result.value must be a Date.
console.log("Date:", result.value.toLocaleString());
} else {
// the type checker knows result.value must be an Error in the else branch.
console.log("Error:", result.value.message);
}
```

[`Option`](http://github.bodil.lol/opt/types/Option.html) in Deno:

```typescript
import { Option, Some } from "https://deno.land/x/opt/option.ts";

const option: Option = Some(new Date());

// option.value is Date | undefined here.

if (option.isSome()) {
// the type checker now knows option.value is definitely a Date.
console.log("Date:", option.value.toLocaleString());
} else {
console.log("No value!");
}
```

## Licence

Copyright 2023 Bodil Stokke

This software is subject to the terms of the Mozilla Public License, v. 2.0. If
a copy of the MPL was not distributed with this file, You can obtain one at
.