Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/kylecorbelli/typescript-maybe

A TypeScript Maybe Type and Module
https://github.com/kylecorbelli/typescript-maybe

elm elm-lang functional-programming haskell javascript maybe maybe-monad typescript

Last synced: about 1 month ago
JSON representation

A TypeScript Maybe Type and Module

Awesome Lists containing this project

README

        

# TypeScript Maybe Type and Module

## Installation

```
$ npm install --save typescript-maybe
```

## Example Usage
```TypeScript
import { Maybe, MaybeType } from 'typescript-maybe'
import { compose, toUpper } from 'ramda'

// An example of an "unsafe" function that may return undefined or null:
function unsafeHead (list: ReadonlyArray): T {
return list[0]
}

// Elegantly handling this unsafe function in a composition chain:
type UpperCaseHead = (list: ReadonlyArray) => Maybe
const otherUpperCaseHead: UpperCaseHead = compose(
Maybe.map(toUpper),
Maybe.of(unsafeHead),
) as UpperCaseHead

// Handling Maybe in a composition chain and returning a default value:
type UpperCaseHeadWithDefault = (list: ReadonlyArray) => string
const upperCaseHeadWithDefault: UpperCaseHead = compose(
Maybe.withDefault('Something didn’t quite go according to plan!'),
Maybe.map(toUpper),
Maybe.of(unsafeHead),
) as UpperCaseHeadWithDefault
```