
Security News
New CVE Forecasting Tool Predicts 47,000 Disclosures in 2025
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
typescript-optional
Advanced tools
Optional (like Java) implementation in TypeScript
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+.
npm install --save typescript-optional
// import Optional type from this module
import Optional from 'typescript-optional';
Optional<T>
objectslet nullableString: string | null = /* some nullable value */;
// all the following variables will be parameterized as Optional<string>.
let optional = Optional.ofNullable(nullableString);
let optionalPresent1 = Optional.ofNullable("foo");
let optionalPresent2 = Optional.ofNonNull("foo"); // accepts non-null value (or else throws TypeError)
let optionalEmpty1: Optional<string> = Optional.empty(); // type hinting required
let optionalEmpty2 = Optional.empty<string>(); // or parameterize explicitly
let optional: Optional<string> = Optional.ofNullable( /* some optional value: null | string */ );
// force to retrieve the payload. (or else throws TypeError.)
// this method is not used match.
optional.get();
// be whether a payload is present or not.
optional.isPresent
// be whether this is empty or not. (negation of `isPresent` property)
optional.isEmpty
// execute the given consumer if a payload is present.
optional.ifPresent(value => console.log(value));
// execute the first argument (consumer) if a payload is present, execute the second argument (emptyAction) otherwise.
optional.ifPresentOrElse(value => console.log(value), () => console.log("empty"));
// filter a payload with additional predicate.
optional.filter(value => value.length > 0);
// map a payload with the given mapper.
optional.map(value => value.length);
// map a payload with the given mapper which returns value wrapped with Optional type.
let powerIfPositive: (x: Number) => Optional<Number>
= x => (x > 0) ? Optional.ofNonNull(x * x) : Optional.empty();
let numberOptional: Optional<number> = Optional.ofNullable(/* some optional value: null | number */)
numberOptional.flatMap(value => powerIfPositive(value));
// return this if this is present, return the given another optional otherwise.
let another: Optional<string> = Optional.ofNullable(/* ... */);
optional.or(another);
// retrieve a payload if this is present, return the given value otherwise.
optional.orElse("bar");
// retrieve a payload if this is present, return a value supplied by the given function otherwise.
optional.orElseGet(() => "bar");
// retrieve a payload if this is present, throws an exception supplied by the given function otherwise.
optional.orElseThrow(() => new Error());
MIT License - LICENSE.md
FAQs
Optional (like Java) implementation in TypeScript
The npm package typescript-optional receives a total of 7,541 weekly downloads. As such, typescript-optional popularity was classified as popular.
We found that typescript-optional demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.