TypeScript Nullable

0.6.0 · active · verified Sun Apr 19

typescript-nullable is a utility library for TypeScript that formalizes the concept of possibly absent values, providing a type-safe and functional approach to handling `null` and `undefined`. It defines a `Nullable<T>` type, which is explicitly `T | null | undefined`, mirroring the `Maybe` type found in functional languages like Haskell or Elm. Beyond the type definition, the library exports a `Nullable` object containing a suite of utility functions designed to interact safely with these potentially absent values. These functions, such as `map`, `withDefault`, `isNone`, and `isSome`, are curried and pure, promoting a functional programming style and enhancing type safety by leveraging TypeScript's type guards. As of version 0.6.0, the library is actively maintained, with incremental updates focusing on API refinements and feature additions, though a specific release cadence is not formally published. Its core value proposition lies in enabling developers to write more resilient code by explicitly managing the presence or absence of values, thereby reducing runtime errors associated with unexpected `null` or `undefined` references and offering a robust alternative to imperative null checks.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `Nullable<T>` type definition and shows how to use key utility functions like `map`, `withDefault`, `isSome`, and `isNone` with examples of currying and TypeScript type guards for safe null handling.

import { Nullable } from 'typescript-nullable';

// Demonstrate Nullable type definition implicitly
type UserName = Nullable<string>;

const userName1: UserName = 'Alice';
const userName2: UserName = null;
const userName3: UserName = undefined;

console.log(`User Name 1: ${userName1}`);
console.log(`User Name 2: ${userName2}`);
console.log(`User Name 3: ${userName3}`);

// Demonstrate utility functions
const toUpperCase = (text: string) => text.toUpperCase();

// Nullable.map - example with curried usage
const mappedName1 = Nullable.map(toUpperCase)(userName1);
const mappedName2 = Nullable.map(toUpperCase)(userName2);
console.log(`Mapped Name 1: ${mappedName1}`);
console.log(`Mapped Name 2: ${mappedName2}`);

// Nullable.withDefault
const displayName1 = Nullable.withDefault('Guest')(userName1);
const displayName2 = Nullable.withDefault('Guest')(userName2);
console.log(`Display Name 1: ${displayName1}`);
console.log(`Display Name 2: ${displayName2}`);

// Nullable.isSome and Nullable.isNone with TypeScript type guards
const potentiallyNullString: Nullable<string> = Math.random() > 0.5 ? 'Hello' : null;

if (Nullable.isSome(potentiallyNullString)) {
  console.log(`Value is present: ${potentiallyNullString.length}`); // TS knows it's a string here
} else {
  console.log('Value is absent.'); // TS knows it's null | undefined here
}

// Explicit currying example
const mapToUpper = Nullable.map(toUpperCase);
console.log(`Curried map result: ${mapToUpper('world')}`);
console.log(`Curried map result (null): ${mapToUpper(null)}`);

view raw JSON →