Option type for Typescript based on the Scala Option type
If you're already familliar with type Option
type, this should work more or less like you would expect it to.
Issues and PRs are welcome if you find any bugs in the code.
yarn add @alanqthomas/option
npm install @alanqthomas/option
Test with watch:
yarn test
Test once:
yarn testOnce
Build Typescript:
yarn build
Generate Docs:
yarn docs
See the TypeDoc documentation for more specific type info.
import { Option, Some, None } from '@alanqthomas/option'
Convenience methods to create new options without using new
. This also means that the real classes behind them are suffixed with an _
, e.g. Option_
.
const someVal = Option(42)
= Some(42)
const noneVal = None
= Option(null)
= Option(undefined)
= Option.empty<WhateverType>()
Option.empty
is useful is you need a None
that conforms to a certain type.
Let's use these values for our examples (A
will be number
):
// Option<Int>
const some = Some(42)
const none = Option.empty<number>()
some.isEmpty() // false
none.isEmpty() // true
some.isDefined() // true
none.isDefined() // false
some.isDefined() // true
none.isDefined() // false
some.get() // 42
none.get() // throws ReferenceError!!
some.getOrElse(0) // 42
none.getOrElse(0) // 0
some.orElse(Some(0)) // Some(42)
none.orElse(Some(0)) // Some(0)
some.map(x => x + 2) // Some(44)
none.map(x => x + 2) // None
some.flatMap(x => Some(x + 2)) // Some(44)
none.flatMap(x => Some(x + 2)) // None
NonEmpty
is a type alias for a non-null, non-undefined value
type NonEmpty = string | number | boolean | symbol | object
some.fold(0, x => x + 2) // 44
none.fold(0, x => x + 2) // 0
some.filter(x => x === 42) // Some(42)
none.filter(x => x === 42) // None
some.filterNot(x => x === 0) // Some(42)
none.filterNot(x => x === 0) // None
some.contains(42) // true
none.contains(42) // false
some.exists(x => x === 42) // true
none.exists(x => x === 42) // false
some.forall(x => x === 42) // true
none.forall(x => x === 42) // true
some.foreach(x => console.log(x)) // prints 42
none.foreach(x => console.log(x)) // doesn't print
some.toArray() // [42]
none.toArray() // []
matches
is an object of shape:
matches: {
none: () => C,
some: (a: A) => B
}
Of course, order doesn't matter in an object, so you can put in the some
or none
function in the order you want
some.match({
some: x => x + 2,
none: () => 0
}) // 44
none.match({
some: x => x + 2,
none: () => 0
}) // 0