sindresorhus/is · error · TypeError

Expected value which is `empty array`, received value of typ

Error message

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

What it means

Thrown by `assertEmptyArray()` when the value fails `isEmptyArray` — it must be a real Array AND have length 0. Both a non-array (even an empty one, like `{}` or an empty Set) and a non-empty array trigger it. Used to enforce an expected empty-state invariant (e.g., no leftover errors, drained queue).

Source

Thrown at source/index.ts:1556

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

export function assertDate(value: unknown, message?: string): asserts value is Date {
	if (!isDate(value)) {
		throw new TypeError(message ?? typeErrorMessage('Date', value));
	}
}

export function assertDirectInstanceOf<T>(instance: unknown, class_: Class<T>, message?: string): asserts instance is T {
	if (!isDirectInstanceOf(instance, class_)) {
		throw new TypeError(message ?? typeErrorMessage('T', instance));
	}
}

export function assertEmptyArray(value: unknown, message?: string): asserts value is never[] {
	if (!isEmptyArray(value)) {
		throw new TypeError(message ?? typeErrorMessage('empty array', value));
	}
}

export function assertEmptyMap(value: unknown, message?: string): asserts value is Map<never, never> {
	if (!isEmptyMap(value)) {
		throw new TypeError(message ?? typeErrorMessage('empty map', value));
	}
}

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

export function assertEmptySet(value: unknown, message?: string): asserts value is Set<never> {
	if (!isEmptySet(value)) {
		throw new TypeError(message ?? typeErrorMessage('empty set', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. If the value is a non-empty array, the assertion is doing its job — inspect the contents to fix the underlying invariant violation (e.g., handle the accumulated errors).
  2. If the container type is wrong, convert first: `[...set]`, `Array.from(nodeList)` before asserting.
  3. If a non-empty array is legitimately possible, replace the assertion with a branch: `if (errors.length > 0) { ... }`.

Example fix

// before
assert.emptyArray(validationErrors); // throws when errors exist
// after — handle rather than assert when non-empty is expected
if (validationErrors.length > 0) {
	throw new AggregateError(validationErrors, 'Validation failed');
}
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(value) || value.length !== 0) {
  // expected an empty array — clear it or fix the producer that populated it
}

Type guard

function isEmptyArray(value: unknown): value is [] {
  return Array.isArray(value) && value.length === 0;
}

Try / catch

try {
  assert.emptyArray(value);
} catch (error) {
  if (error instanceof TypeError) {
    // either not an array at all, or an array with elements — the message's type name tells you which
  } else throw error;
}

Prevention

When it happens

Trigger: Calling `assert.emptyArray(value)` with an array that still has elements (invariant violation), or with array-likes: an empty Set/Map, arguments object, NodeList, typed array, or a JSON-parsed object `{}`.

Common situations: Validation error collectors asserted empty at the end of a run while errors accumulated; queues expected drained but a race left items; using Set/NodeList where Array was assumed; test assertions expecting a cleanup step to have cleared state.

Related errors


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