Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bodil/opt
Option types for TypeScript with real gradual typing
https://github.com/bodil/opt
Last synced: about 1 month ago
JSON representation
Option types for TypeScript with real gradual typing
- Host: GitHub
- URL: https://github.com/bodil/opt
- Owner: bodil
- License: mpl-2.0
- Created: 2023-05-10T19:07:00.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-04T11:07:31.000Z (about 1 month ago)
- Last Synced: 2024-10-04T11:10:11.449Z (about 1 month ago)
- Language: TypeScript
- Homepage: http://github.bodil.lol/opt/
- Size: 47.9 KB
- Stars: 20
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
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
.