TypeScript Optional
Optional (like Java) implementation in TypeScript
Overview
Optional<T>
is a type which may or may not contain a payload of type T
.
It provides a common interface regardless of whether an instance is present or is empty.
This module is inspired by Optional class in Java 8+.
The following methods are currently not supported:
equals
toString
hashCode
stream
Install
npm install --save typescript-optional
Usage
import
import { Optional } from "typescript-optional";
creating Optional<T>
objects
const nullableString: string | null = ;
const optional = Optional.ofNullable(nullableString);
const optionalPresent1 = Optional.ofNullable("foo");
const optionalPresent2 = Optional.ofNonNull("foo");
const optionalEmpty1: Optional<string> = Optional.empty();
const optionalEmpty2 = Optional.empty<string>();
operations
const optional: Optional<string> = Optional.ofNullable( );
optional.get();
optional.isPresent();
optional.isEmpty();
optional.ifPresent(value => console.log(value));
optional.ifPresentOrElse(value => console.log(value), () => console.log("empty"));
optional.filter(value => value.length > 0);
optional.map(value => value.length);
const powerIfPositive: (x: Number) => Optional<Number>
= x => (x > 0) ? Optional.ofNonNull(x * x) : Optional.empty();
const numberOptional: Optional<null | number> = Optional.ofNullable()
numberOptional.flatMap(value => powerIfPositive(value as number));
const another: Optional<string> = Optional.ofNullable();
optional.or(another);
optional.orElse("bar");
optional.orElseGet(() => "bar");
optional.orElseThrow(() => new Error());
optional.orNull();
optional.orUndefined();
optional.matches({
present: value => value.length,
empty: () => 0,
})
optional.toOption();
prototype-free types
While Optional
's fluent interface for method chaining with prototype
is usually useful and elegant,
relying on prototype
can cause some problems in certain situations like that an external function copies such objects except prototype
.
For example, setState
of React reflects the given value as a state except the value's prototype
(and then you will see "TypeError: undefined is not a function" in runtime though TypeScript compilation has been succeeded!).
To avoid this issue, you have three options that convert an Optional
into a prototype-free, or a simple JavaScript object (associative array, string etc.).
Optional.orNull
Optional.orUndefined
Optional.toOption
Optional.orNull
and Optional.orUndefined
Using Optional.orNull
or Optional.orUndefined
is the simple way to obtain prototype-free objects.
These methods convert an Optional<T>
into a value of type union.
Optional<T>.orNull
returns T | null
.
Optional<T>.orUndefined
returns T | undefined
. The T | undefined
type is compatible with optional parameters and properties of TypeScript.
Use Optional.ofNullable
to restore an Optional value from a value of these type unions.
const update: <T> (original: T) => T =
const optional: Optional<string> = ;
let nullable: string | null = optional.orNull();
let orUndefined: string | undefined = optional.orUndefined();
nullable = update(nullable);
orUndefined = update(orUndefined);
const optionalFromNullable: Optional<string> = Optional.ofNullble(nullable);
const optionalFromOrUndefined: Optional<string> = Optional.ofNullble(orUndefined);
Option.toOption
As a more explicit way to obtain prototype-free objects, Optional.toOption
is provided.
This method convert an Optional<T>
into an object of Option<T>
type, which conforms to discriminated unions also known as algebraic data types.
Refer the API document of Option<T>
to learn about the structure.
const update: <T> (original: Option<T>) => T =
const optional: Optional<string> = ;
let option: Option<string> = optional.toOption();
option = update(option);
const optionalFromOption: Optional<string> = Optional.from(option);
License
MIT License - LICENSE.md