JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 334
  • Score
    100M100P100Q85724F
  • License MIT

Correct, easy to use Option type for TypeScript

Package Exports

  • tsoption

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tsoption) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

tsoption Build Status npm mit fantasy

Correct, easy to use Option type for TypeScript. Like Scala options; see the introductory blog post.

Installation

# Using Yarn:
yarn add tsoption

# Or, using NPM:
npm install tsoption --save

Usage

Note: You can use JavaScript instead of TypeScript, but it's not as fun.

let a = Option.from(3)                // Some<number>(3)
  .flatMap(_ => Some(5))              // Some<number>(5)
  .map(_ => 'a')                      // Some<string>('a')
  .orElse(Option.from('b'))           // Some<string>('a')   (non-string type gives a compile error)
  .getOrElse('c')                     // 'a'

let b = Option.from('a')              // Some<string>('a')
  .flatMap(_ => None())               // None<string>()      (flatMap can map to any type)
  .orElse(Option.from('b'))           // Some<string>('b')   (non-string type gives a compile error)
  .get()                              // 'b'

API

Note: The types of each of the expressions below are known at compile time.

// Create an option
Option.from(3)                        // Some(3)
Option.from('abc')                    // Some('abc')
Option.from(null)                     // None                (for convenience)
Option.from(undefined)                // None                (for convenience)
Some(3)                               // Some(3)
Some(null)                            // Some(null)
None()                                // None

// #flatMap
Some(3).flatMap(_ => Some(_ * 2))     // Some(6)
None().flatMap(() => Some(2))         // None

// #get
Some(3).get()                         // 3
None().get()                          // COMPILE ERROR! Can't call get() on None

// #getOrElse
Some(1).getOrElse(2)                  // 1
None().getOrElse(2)                   // 2

// #isEmpty
Some(2).isEmpty()                     // false (known at compile time too!)
None().isEmpty()                      // true (known at compile time too!)

// #map
Some(2).map(_ => _ * 2)               // Some(4)
None().map(() => 2)                   // None (known at compile time too!)

// #nonEmpty
Some(2).nonEmpty()                    // true (known at compile time too!)
None().nonEmpty()                     // false (known at compile time too!)

// #orElse
Some(2).orElse(Option.from(3))        // Some(2)
None().orElse(Option.from(3))         // Some(3)

// #toString
Some(2).toString()                    // "Some(2)"
None().toString()                     // "None"

Fantasyland

TSOption is Fantasyland-compliant. It implements:

Fantasyland-Compatible API

import * as fl from 'fantasy-land'
const {ap, chain, map, of} = fl

// Create an option
Option[of](3)                                // Some(3)
Option[of]('abc')                            // Some('abc')
Option[of](null)                             // None
Option[of](undefined)                        // None
Option[of](3).constructor[of](4)             // Some(4)
Option[of](3).constructor[of](null)          // None

// #chain
Option[of](3)[chain](_ => Option[of](_ * 2)) // Some(6)
Option[of](null)[chain](() => Option[of](2)) // None  (known at compile time too!)

// #map
Option[of](2)[map](_ => _ * 2)               // Some(4)
Option[of](null)[map](() => 2)               // None  (known at compile time too!)

// #ap
Option[of](2)[ap](Option[of](_ => _ * 2))    // Some(4)
Option[of](null)[ap](Option[of](() => 2))    // None  (known at compile time too!)

Tests

npm test

License

MIT