This is a somewhat naive implementation of ADTs for typescript.
none(): None
create an instance ofOption<T>
that holds no value (unit type)some<T>(value: T): Some<T>
create an instance ofOption<T>
that holds a valuetype Option<T> = None | Option<T>
isNone(value)
- is provided argument of typeNone
getOrElse<T>(option: Option<T>, default: T): T
- get an optional value if exists, fallback to default otherwisefromNullable<T>(value: T | null | undefined): Option<T>
create anOption<T>
from a possibly nullable value, where nullish becomeNone
and other values becomeSome<T>
Let's say we have a sparse array of numbers. We'd like to sum it, but some members of the array might be nullish. We can't use falsy values as indication (because 0), so let's try to use options instead.
import { option } from "@oakfang/adt";
const numbers: Array<null | number> = [
/*...*/
];
const sum = numbers
// Array<null | number> -> Array<Option<number>>
.map(fromNullable)
// Array<Option<number>> -> number[]
.map((o) => getOrElse(o, 0))
// number[] -> number
.reduce((x, y) => x + y, 0);
Some function might throw, but the issue with that is that neither JS nor TS provide us with a way to account for these thrown errors in a type-safe manner. The Result
type aims to remove the need for thrown exception, as it encapsulates both a function that follows the "happy flow" (i.e., returns) and one that doesn't.