https://github.com/sajmoni/ts-elm
Elm types in TypeScript
https://github.com/sajmoni/ts-elm
elm fp functional-programming result typescript
Last synced: 8 months ago
JSON representation
Elm types in TypeScript
- Host: GitHub
- URL: https://github.com/sajmoni/ts-elm
- Owner: sajmoni
- License: mit
- Created: 2021-04-23T19:42:38.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-02-10T09:08:35.000Z (over 2 years ago)
- Last Synced: 2024-01-23T12:02:33.160Z (almost 2 years ago)
- Topics: elm, fp, functional-programming, result, typescript
- Language: TypeScript
- Homepage:
- Size: 610 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ts-elm
Elm types in TypeScript
Implements [Elm's](https://elm-lang.org/) [`Result`](https://package.elm-lang.org/packages/elm/core/latest/Result) type in TypeScript. Use it to make error handling explicit.
Instead of throwing errors, you create Results with the `ok` and `err` functions.
## :sparkles: Benefits
- Type signatures indicate if a function can fail or not
- Ensures that errors are always handled
- Work with the data as if it always succeeds and add error handling where it makes the most sense
- Fully typed
- Zero dependencies
---
## :wrench: Example usage
---
## :package: Install
```sh
npm install ts-elm
```
---
## :newspaper: API
The goal of the API is to be as similar to the original Elm code as possible.
There is purposefully no type guards to detect if a `Result` is `Ok` or `Err`. This is to force the use
of `match`, which ensures that Errors are always handled.
### Type and Constructors
```ts
type Result = Ok | Err
```
A Result is either Ok meaning the computation succeeded, or it is an Err meaning that there was some failure.
### Mapping
```ts
type map = ((x: any) => any, result: Result) => Result
```
Apply a function to a result. If the result is Ok, it will be converted. If the result is an Err, the same error value will propagate through.
Example:
```ts
map(Math.sqrt, ok(4)) // ok(2)
map(Math.sqrt, err('Bad input')) // err('Bad input')
```