CxJS

isDefined

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

Type guard that checks if a value is not undefined. Note: null is considered defined.

Signature

function isDefined<T>(value: T | undefined): value is T

Examples

isDefined("hello");        // true
isDefined(0);              // true
isDefined(false);          // true
isDefined(null);           // true (null is defined)
isDefined(undefined);      // false
isDefined(void 0);         // false

Use Cases

function getConfig(options?: { timeout?: number }) {
  if (isDefined(options?.timeout)) {
    // Use the provided timeout
    return { timeout: options.timeout };
  }
  // Use default
  return { timeout: 5000 };
}

See Also