https://github.com/joakin/sum-types
Sum types for JS
https://github.com/joakin/sum-types
Last synced: 8 months ago
JSON representation
Sum types for JS
- Host: GitHub
- URL: https://github.com/joakin/sum-types
- Owner: joakin
- Created: 2018-04-12T21:43:36.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-15T10:23:09.000Z (over 7 years ago)
- Last Synced: 2024-10-16T19:26:40.694Z (over 1 year ago)
- Language: JavaScript
- Size: 52.7 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @joakin/sum-types
```
npm install @joakin/sum-types
```
`sum-types` provides union|adt|sum types for JS.
Exhaustiveness checking, `Maybe` and `Result` types, and a nice API.
[Read the docs](https://joakin.github.io/sum-types)
```js
import { Type } from "@joakin/sum-types";
const State = Type({
Loading: [],
Success: ["data"],
Error: ["error"]
});
let state1 = State.Loading();
let state2 = State.Success(42);
let state3 = State.Error(new Error("Failed"));
function toString(state) {
return state.match({
Loading: _ => "Loading",
Success: data => `Got data ${data}`,
Error: err => `Got error: ${err}`
});
}
// Or
const toString = State.match({
Loading: _ => "Loading",
Success: data => `Got data ${data}`,
Error: err => `Got error: ${err}`
});
```