is-not-null-or-undefined
v2.0.0
Published
TypeScript predicates and type guards for nullish values, booleans, non-empty values, and renderable React nodes
Downloads
297
Maintainers
Readme
is-not-null-or-undefined
TypeScript predicates and type guards for nullish values, booleans, non-empty values, non-blank strings, and renderable React nodes.
Installation
npm
npm install is-not-null-or-undefinedyarn
yarn add is-not-null-or-undefinedpnpm
pnpm add is-not-null-or-undefinedUsage
import {
isNotNullOrUndefined,
isNotNullOrUndefinedAndFalse,
isNotNullOrUndefinedAndNotBlank,
isNotNullOrUndefinedAndNotEmpty,
isNotNullOrUndefinedAndTrue,
isNullOrUndefinedOrFalse,
} from 'is-not-null-or-undefined';
// Returns true for non-`null` and non-`undefined` values
isNotNullOrUndefined('string'); // true
isNotNullOrUndefined(123); // true
isNotNullOrUndefined({}); // true
isNotNullOrUndefined([]); // true
isNotNullOrUndefined(false); // true
isNotNullOrUndefined(0); // true
// Returns false for `null` and `undefined` values
isNotNullOrUndefined(null); // false
isNotNullOrUndefined(undefined); // false
// Checks strings, arrays, and array-like values by length
isNotNullOrUndefinedAndNotEmpty('value'); // true
isNotNullOrUndefinedAndNotEmpty(' '); // true
isNotNullOrUndefinedAndNotEmpty(''); // false
isNotNullOrUndefinedAndNotEmpty([1]); // true
isNotNullOrUndefinedAndNotEmpty([]); // false
// Checks strings after trimming
isNotNullOrUndefinedAndNotBlank(' value '); // true
isNotNullOrUndefinedAndNotBlank(' '); // false
// Checks boolean values
isNotNullOrUndefinedAndTrue(true); // true
isNotNullOrUndefinedAndTrue(false); // false
isNotNullOrUndefinedAndFalse(false); // true
isNullOrUndefinedOrFalse(null); // true
isNullOrUndefinedOrFalse(false); // true
isNullOrUndefinedOrFalse(true); // falseType Guard
The function acts as a TypeScript type guard, narrowing the type from T | null | undefined to T:
const value: string | null | undefined = getValue();
if (isNotNullOrUndefined(value)) {
// value is now typed as string
console.log(value.toUpperCase());
}React Subpath
import { hasRenderableNode } from 'is-not-null-or-undefined/react';
hasRenderableNode('value'); // true
hasRenderableNode(0); // true
hasRenderableNode([null, 'value']); // true
hasRenderableNode(new Set([false, 'value'])); // true
hasRenderableNode(''); // false
hasRenderableNode(true); // false
hasRenderableNode(false); // false
hasRenderableNode([]); // false
hasRenderableNode(new Set([false, true, ''])); // false
hasRenderableNode(null); // false
hasRenderableNode(undefined); // falseThe /react subpath has no React runtime dependency. It imports ReactNode as a type only. TypeScript consumers of this subpath should have React types installed. hasRenderableNode returns a boolean predicate rather than a TypeScript type guard.
Migrating from v1 to v2
Version 2 is ESM-only and supports Node.js >=22.14.0. Import helpers by name from the package root or from the /react subpath.
import { isNotNullOrUndefined } from 'is-not-null-or-undefined';
import { hasRenderableNode } from 'is-not-null-or-undefined/react';Default imports are not supported. Use the named isNotNullOrUndefined export instead.
The root entrypoint now includes focused helpers for non-empty values, non-blank strings, boolean guards, and false | null | undefined checks. React-specific render checks live behind the /react subpath so the root runtime stays React-free.
Agent-Assisted Adoption
This package includes an optional Agent Skill for reviewing a codebase for checks that may be clearer with these utilities.
Skill path:
skills/find-nullish-utils/SKILL.mdYou can ask a coding agent to use that skill when reviewing nullable checks, boolean/nullish checks, empty string checks, array length checks, or React render guards.
The skill is guidance, not an automatic codemod. Suggested changes should still be reviewed for local semantics before applying them.
