Standard Toolkit
Packages@accelint/predicates

isNothing

Type guard that determines if a value is null or undefined

Usage

import { isNothing } from '@accelint/predicates/is-nothing';

isNothing(null);       // true
isNothing(undefined);  // true
isNothing(0);          // false
isNothing('');         // false
isNothing(false);      // false

Reference

function isNothing(val: unknown): boolean

Parameters

ParameterTypeDescription
valunknownValue to check

Returns

Returns true if the value is null or undefined, false otherwise.

Examples

Example: Early return for missing values

import { isNothing } from '@accelint/predicates/is-nothing';

function processUser(user: User | null | undefined) {
  if (isNothing(user)) {
    console.log('No user provided');
    return;
  }
  
  // Safe to use user here
  console.log(user.name);
}

Example: Filtering out null/undefined from arrays

import { isNothing } from '@accelint/predicates/is-nothing';

const values = [1, null, 2, undefined, 3, 0, false];
const notNull = values.filter(v => !isNothing(v));
// [1, 2, 3, 0, false]

Example: Validating required fields

import { isNothing } from '@accelint/predicates/is-nothing';

interface FormData {
  name?: string;
  email?: string;
  phone?: string;
}

function validateRequired(data: FormData): boolean {
  return !isNothing(data.name) && !isNothing(data.email);
}

validateRequired({ name: 'John', email: 'john@example.com' }); // true
validateRequired({ name: 'John' }); // false
validateRequired({ email: 'john@example.com' }); // false

Example: Default value assignment

import { isNothing } from '@accelint/predicates/is-nothing';

function getConfigValue(value: string | null | undefined): string {
  if (isNothing(value)) {
    return 'default-config';
  }
  return value;
}

getConfigValue(null);      // "default-config"
getConfigValue(undefined); // "default-config"
getConfigValue('custom');  // "custom"
getConfigValue('');        // "" (empty string is not nothing)

Good to know: Uses loose equality (==) rather than strict equality (===), so it matches both null and undefined with a single comparison. This is one of the rare cases where loose equality is preferred for brevity.

  • isSomething - Opposite check: value is not null or undefined
  • isString - Check if value is a string
  • isNumber - Check if value is a number

On this page