CxJS

isArray

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

Type guard that checks if a value is an array.

Signature

function isArray(value: any): value is any[]

Examples

isArray([1, 2, 3]);        // true
isArray([]);               // true
isArray("hello");          // false
isArray({ length: 3 });    // false
isArray(null);             // false
isArray(undefined);        // false

Use Cases

function processItems(input: unknown) {
  if (isArray(input)) {
    // TypeScript knows input is any[] here
    return input.map(item => process(item));
  }
  return [process(input)];
}

See Also