Skip to content

isArray function ​

Checks whether a value is an array, whatever its items.

ts
import { isArray } from 'utils';

Signature ​

ts
export function isArray(value: unknown): value is unknown[]

Parameters ​

NameDescription
valueThe value to check.

Returns ​

true for an array; false for array-likes such as strings or typed arrays.

Example ​

ts
isArray([1, 2]); // true
isArray({ length: 0 }); // false

Tests ​

Line coverage: 100 %.

  • ✅ accepts arrays only

Source ​

src/guard/is-array.ts (on GitHub)

Source code
ts
/**
 * Checks whether a value is an array, whatever its items.
 *
 * @param value - The value to check.
 * @returns `true` for an array; `false` for array-likes such as strings or typed arrays.
 * @example
 * isArray([1, 2]); // true
 * isArray({ length: 0 }); // false
 */
export function isArray(value: unknown): value is unknown[] {
  return Array.isArray(value);
}
Test file
ts
import { isArray } from './is-array';

describe(isArray, () => {
  it('accepts arrays only', () => {
    expect(isArray([])).toBe(true);
    expect(isArray({ length: 0 })).toBe(false);
    expect(isArray('abc')).toBe(false);
    expect(isArray(null)).toBe(false);
  });
});

Keywords: is, array, guards, function

Generated from the JSDoc of the sources.