is-not-null-or-undefined
v1.0.2
Published
A TypeScript utility function to check if a value is not null or undefined
Maintainers
Readme
is-not-null-or-undefined
A TypeScript utility function & type-guard to check if a value is not null or undefined.
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 } 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); // 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());
}