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

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

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')
```