A TypeScript utility function & type-guard to check if a value is not null or undefined.
npm install is-not-null-or-undefinedyarn add is-not-null-or-undefinedpnpm add is-not-null-or-undefinedimport { 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); // falseThe 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());
}