CxJS

isUndefined

import { isUndefined } from 'cx/util'; Copied

The isUndefined type guard checks if a value is strictly undefined.

Basic Usage

import { isUndefined } from "cx/util";

isUndefined(undefined); // true
isUndefined(void 0); // true
isUndefined(null); // false
isUndefined(""); // false
isUndefined(0); // false
isUndefined(false); // false

Type Narrowing

The function is a TypeScript type guard that narrows the type to undefined.

import { isUndefined } from "cx/util";

function process(value: string | undefined): string {
  if (isUndefined(value)) {
    return "default";
  }
  return value; // typed as string
}

vs isDefined

isUndefined is the inverse of isDefined.

import { isUndefined, isDefined } from "cx/util";

const value: string | undefined = getValue();

if (isUndefined(value)) {
  // value is undefined
}

if (isDefined(value)) {
  // value is string
}

API

function isUndefined(x: any): x is undefined;
ParameterTypeDescription
xanyThe value to check

Returns: true if the value is strictly undefined.

See Also

  • isDefined - Check if value is not undefined