Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/kylecorbelli/typescript-maybe
- Owner: kylecorbelli
- Created: 2018-05-02T23:35:09.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-05-05T18:04:48.000Z (over 6 years ago)
- Last Synced: 2024-11-14T16:55:43.679Z (about 1 month ago)
- Topics: elm, elm-lang, functional-programming, haskell, javascript, maybe, maybe-monad, typescript
- Language: TypeScript
- Homepage: http://kylecorbelli.com/index.php/2018/05/01/typescript-maybe-type-module/
- Size: 51.8 KB
- Stars: 3
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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
```