Null-Object (npm package)
A runtime-safe, polymorphic noop object that absorbs all method calls, property accesses, and assignments—without side effects or the need for conditionals.
Replace Optional Dependencies with a Silent Stand-In 1
Motivation
Applications often rely on optional services—loggers, plugins, metrics—that may be disabled or unavailable in certain environments. Instead of littering the codebase with if
checks, this utility provides:
- Clean fallbacks: Replace real implementations with a zero-behavior object.
- Type-safe defaults: Maintain proper interfaces while safely doing nothing.
- Simpler runtime logic: Avoid conditional branching and reduce code noise.
Key Features
- Universal trap – Safely handles any method, property, or nested access.
- Chaining support – Always returns itself to enable method chaining.
- Assignment-safe – Accepts any property assignment silently.
noop()
bundled – A utility no-operation function included for convenience.- TypeScript-native – Fully typed interface with JSDoc support.
- No dependencies – Lightweight, production-ready.
Technology Highlights
- TypeScript-native: Strictly typed, using TypeScript 5.8 with Node 20 config
- ESM-first: Modern module output (
"type": "module"
) for nativeimport/export
- Proxy-based: Polymorphic behavior via JavaScript’s
Proxy
API - Zero dependencies: Lightweight runtime with no external packages
Tooling & Pipeline
- Vitest: Unit tests with V8 coverage
- ESLint: Linting with TS rules
- Typed builds:
.d.ts
output + stricttsconfig
- Readme checks: Automated Markdown link validation
- Pre-publish checks: Type check, lint, test, usage validation
Footnotes
-
Kerievsky J., Refactoring to Patterns (2004), p. 343 Null Object; Fowler M., Refactoring (2018), p. 289 under "Introduce Special Case" title. ↩