typescript-optional provides an implementation of the `Optional<T>` type, inspired by Java 8+'s `Optional` class, designed to help developers manage the presence or absence of a value without resorting to null or undefined checks. It aims to reduce `NullPointerExceptions` (or `TypeError` in JavaScript) by providing a fluent API for handling nullable values. The current stable version is 2.0.1, though a 3.0.0-alpha.3 pre-release is available, indicating active development. The package has seen irregular release cycles, with a previous 2.0.0 release being abandoned due to deployment issues before 2.0.1 stabilized it. Key differentiators include its strong typing with TypeScript, direct inspiration from Java's `Optional` API (e.g., `isPresent`, `map`, `orElse`), and methods like `orNull()` and `orUndefined()` for easy conversion back to native JavaScript nullable types. It focuses on the core `Optional` functionality, explicitly noting missing methods like `equals` or `toString` compared to its Java counterpart.
npm install typescript-optionalVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to import, create, and perform common operations like checking presence, conditional execution, mapping, filtering, and providing default values with the `Optional` class.
Update calls like `optional.isPresent` to `optional.isPresent()` and `optional.isEmpty` to `optional.isEmpty()`.
Always check `optional.isPresent()` before calling `get()`, or use safer methods like `orElse()`, `orElseGet()`, `orElseThrow()`, `orNull()`, or `orUndefined()` to retrieve the value.
Use `Optional.ofNullable(value)` instead if the value might be `null` or `undefined`. Only use `ofNonNull` when you are certain the value is not null.
Upgrade to `typescript-optional@2.0.1` or a later compatible version.
Change `optional.isPresent` to `optional.isPresent()` and `optional.isEmpty` to `optional.isEmpty()`.
Before calling `get()`, verify the presence of a value with `optional.isPresent()`, or use alternative methods like `orElse()`, `orElseGet()`, `orNull()`, or `orUndefined()` to safely handle empty optionals.
If the value might be `null` or `undefined`, use `Optional.ofNullable(value)` instead. Only use `Optional.ofNonNull()` when you are absolutely sure the value is present.
No dependency data recorded yet.