
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
typescript-optional
Advanced tools
Optional (like Java) implementation in TypeScript
Optional<T>
is a type which may contain a payload of type T
or may not contain.
It provides the common interface regardless of an instance is present or is empty.
This module reffers to 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 */ );
// 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
// if a payload is present, execute the given procedure.
optional.ifPresent(value => console.log(value));
// 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();
optional.flatMap(value => powerIfPositive(value));
// retrieve a payload if this is present, return the given value or else.
optional.orElse("bar");
// retrieve a payload if this is present, return a value supplied by the given function or else.
optional.orElseGet(() => "bar");
// retrieve a payload if this is present, throws an exception supplied by the given function or else.
optional.orElseThrow(() => new Error());
MIT License - LICENSE.md
FAQs
Optional (like Java) implementation in TypeScript
The npm package typescript-optional receives a total of 8,403 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
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.