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.
npm install typescript-nullableVerified import paths — ran on the pinned version, not inferred.
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.
Always pin to exact versions (e.g., `"typescript-nullable": "~0.6.0"` or `"^0.6.0"` carefully) and review release notes for any new updates.
Ensure you call curried functions correctly, either by providing all arguments at once (e.g., `Nullable.map(func, value)`) or by explicitly chaining calls (e.g., `Nullable.map(func)(value)`).
Be mindful that `Nullable` refers to both the type and the value-level object. TypeScript handles this distinction correctly in most contexts, but avoid shadowing or ambiguous variable names.
Utility functions are properties of the `Nullable` object. Import `Nullable` and then access the functions: `import { Nullable } from 'typescript-nullable'; Nullable.isSome(value);`Ensure you are using `import { Nullable } from 'typescript-nullable';` for ESM environments. If in CommonJS, try `const { Nullable } = require('typescript-nullable');` or `const Nullable = require('typescript-nullable').Nullable;`The `None` type is an internal alias for `null | undefined` and is not exported. You should use `null | undefined` directly or define your own `type None = null | undefined;` if you wish to use that alias in your code.
No dependency data recorded yet.