Option - API
Option represents an optional value: it is either some and contains a value, or none, and does not.
In contrast with the full-fledged Option<T>
from fp-tp, this module provides just the bare minimum. If you require more features, consider using fp-tp.
Installation
- npm
- yarn
- pnpm
npm is the default package manager for Node.js, and to where tscommon is published.
Your project is using npm if it has a
Run the following command in your terminal:
Your project is using npm if it has a
package-lock.json
file in its root folder.Run the following command in your terminal:
terminal
npm install @tscommon/option
Usage
- Represent optional value
- Unwrap at least one array item
- Unwrap all array items
main.ts
import { None, Option, Some } from '@tscommon/option';
let data: Option<number> = Option.some(42);
if (Option.is(data, Some)) {
// Data is present
}
data = Option.none;
if (Option.is(data, None)) {
// Data is absent
}
main.ts
import { Option } from '@tscommon/option';
Option.any([Option.some(1), Option.some(2)]);
// Output:
// Some { value: 1 }
Option.any([Option.some(1), Option.none]);
// Output:
// Some { value: 1 }
main.ts
import { Option } from '@tscommon/option';
Option.all([Option.some(1), Option.some(2)]);
// Output:
// Some { value: [1, 2] }
Option.all([Option.some(1), Option.none]);
// Output:
// None {}