ts-optional-class
v1.0.3
Published
Optional Type in TypeScript like Java Optional API
Downloads
4
Readme
Optional
Optional for Typescript inspired by the Java Optional.
Install
npm install --save ts-optional-class
Usage
Example:
import {Optional} from '@ama-team/optional'
const fraudulent = Optional
.of(user)
.map(user => user.processors)
.map(processors => processors.fraud)
.map(metadata => metadata.fraudulent)
.orElse(false);
API
You have four ways to create an optional:
Optional.empty();
Optional.of(value); // throws error if value is null
Optional.ofNullable(value);
Optional.ofAnything(value); // throws error if value is undefined