Option Type for JavaScript
This package provides a robust implementation of the option (or maybe) type for JavaScript, designed to manage optional values and mitigate the pervasive issues of `null` and `undefined`. It offers a functional approach to handling the presence or absence of a value through `some(value)` and `none` constructs, similar to types found in languages like Rust or Scala. The current stable version is `0.2.4`, which was published approximately nine years ago, indicating that the package is no longer actively maintained. Key differentiators include its rich API for value manipulation, such as `map`, `flatMap`, `filter`, `orElse`, and `valueOrElse`, which promote safer and more expressive code compared to traditional null checks. Its `fromNullable` function also provides a convenient way to convert potentially nullish values into `option` types.
Common errors
-
TypeError: Cannot read properties of null (reading 'value') OR TypeError: Cannot read property 'value' of undefined
cause Attempting to access `.value()` on an `option.none` instance after it was converted implicitly or explicitly from `null` or `undefined`.fixRefactor code to use safe unwrapping methods like `option.valueOrElse(defaultValue)` or `option.map(func).valueOrElse(defaultValue)` instead of direct `.value()` calls on potentially empty options. -
Error: value of none()
cause Directly calling `.value()` on an `option.none` instance, which is explicitly designed to throw an error in this scenario.fixUse methods that handle the 'none' case gracefully, such as `.map()`, `.flatMap()`, or `.valueOrElse(defaultValue)` to provide a fallback value or transform the option without directly unwrapping it when it might be empty.
Warnings
- abandoned The `option` package (mwilliamson/node-options) has not been updated in approximately nine years (last published version 0.2.4). It is considered abandoned and may have unaddressed bugs or security vulnerabilities.
- gotcha Attempting to call `.value()` directly on an `option.none` instance will throw a runtime error. This design choice forces explicit handling of the 'none' case, but can lead to unhandled exceptions if not properly anticipated.
- gotcha This package is written in CommonJS and does not provide native TypeScript typings or explicit ESM (ECMAScript Modules) support. Integrating it into modern TypeScript or ESM-first projects may require manual type declarations or CJS interoperability layers.
Install
-
npm install option -
yarn add option -
pnpm add option
Imports
- option
import option from 'option';
const option = require('option'); - option.some
import { some } from 'option';const someValue = option.some('hello'); - option.none
import { none } from 'option';const noValue = option.none;
- option.fromNullable
const maybeValue = option.fromNullable(someNullableVar);
Quickstart
const option = require("option");
// Creating Option instances
const someUser = option.some({
id: 1,
name: () => "Alice"
});
const noUser = option.none;
// Basic checks
console.log(`someUser isSome: ${someUser.isSome()}`); // true
console.log(`noUser isNone: ${noUser.isNone()}`); // true
// Retrieving values safely
function greet(userOption) {
return userOption.map(user => user.name())
.valueOrElse("Anonymous");
}
console.log(`Greeting someUser: Hello ${greet(someUser)}`); // Hello Alice
console.log(`Greeting noUser: Hello ${greet(noUser)}`); // Hello Anonymous
// Using fromNullable
const nullableString = Math.random() > 0.5 ? "A string" : null;
const safeOption = option.fromNullable(nullableString);
console.log(`From nullable: ${safeOption.valueOrElse("No string provided")}`);