sindresorhus/is · error · TypeError

Expected value which is `non-empty array`, received value of

Error message

Expected value which is `non-empty array`, received value of type `${is(value)}`.

What it means

Thrown by `assertNonEmptyArray()` (source/index.ts:1741) in the @sindresorhus/is library when the checked value fails `isNonEmptyArray()`. The assertion requires the value to be an actual Array with `length > 0`; on failure it throws a TypeError whose message interpolates the detected type of the received value. The assert form narrows the value to the tuple type `[Item, ...Item[]]` for TypeScript, so it must throw instead of returning.

Source

Thrown at source/index.ts:1743

		throw new TypeError(message ?? typeErrorMessage('negative integer', value));
	}
}

export function assertNegativeNumber(value: unknown, message?: string): asserts value is number {
	if (!isNegativeNumber(value)) {
		throw new TypeError(message ?? typeErrorMessage('negative number', value));
	}
}

export function assertNodeStream(value: unknown, message?: string): asserts value is NodeStream {
	if (!isNodeStream(value)) {
		throw new TypeError(message ?? typeErrorMessage('Node.js Stream', value));
	}
}

export function assertNonEmptyArray<T = unknown, Item = unknown>(value: T | Item[], message?: string): asserts value is [Item, ...Item[]] {
	if (!isNonEmptyArray(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-empty array', value));
	}
}

export function assertNonEmptyMap<Key = unknown, Value = unknown>(value: unknown, message?: string): asserts value is Map<Key, Value> {
	if (!isNonEmptyMap(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-empty map', value));
	}
}

export function assertNonEmptyObject<Key extends keyof any = string, Value = unknown>(value: unknown, message?: string): asserts value is Record<Key, Value> {
	if (!isNonEmptyObject(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-empty object', value));
	}
}

export function assertNonEmptySet<T = unknown>(value: unknown, message?: string): asserts value is Set<T> {
	if (!isNonEmptySet(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-empty set', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Ensure the array is populated before asserting — guard with `if (is.nonEmptyArray(value))` if empty is a legitimate state instead of asserting.
  2. Convert array-likes with `Array.from(value)` or `[...value]` before the assertion.
  3. Trace why the producer returned an empty array (over-strict filter, empty query result, wrong config key) and fix the source.
  4. Pass a custom `message` argument to make the failure context clearer at the call site.

Example fix

// before
const items = data.results.filter(r => r.active);
assert.nonEmptyArray(items); // throws when all filtered out
// after
const items = data.results.filter(r => r.active);
if (!is.nonEmptyArray(items)) {
  return []; // empty is a valid state here
}
process(items);
Defensive patterns

Strategy: validation

Validate before calling

if (Array.isArray(value) && value.length > 0) {
  assert.nonEmptyArray(value);
}

Type guard

function isNonEmptyArray<T>(v: unknown): v is [T, ...T[]] {
  return Array.isArray(v) && v.length > 0;
}

Try / catch

try {
  assert.nonEmptyArray(items);
} catch (error) {
  if (error instanceof TypeError) {
    // items missing or empty — supply required data or reject the request
  }
  throw error;
}

Prevention

When it happens

Trigger: Calling `assert.nonEmptyArray(value)` (or `assertNonEmptyArray`) with `[]`, with a non-array such as a string, Set, arguments object, typed array, or array-like `{length: 1}`, or with `undefined`/`null` from a missing input.

Common situations: Validating function arguments or API payloads where an upstream query, `.filter()`, or JSON parse unexpectedly produced an empty array; passing an array-like (NodeList, arguments) instead of a real array; config fields that default to empty lists.

Related errors


AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31). Data as JSON: /data/errors/1427fb29aa4cfb53.json. Report an issue: GitHub ↗.