sindresorhus/is · error · TypeError

Expected value which is `empty map`, received value of type

Error message

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

What it means

Thrown by `assertEmptyMap` (source/index.ts:1560) in the sindresorhus `@sindresorhus/is` library when the given value is not a Map with size 0. The message interpolates `is(value)` to report the actual detected type. Assert functions in this library throw a TypeError instead of returning false, giving TypeScript an `asserts value is Map<never, never>` narrowing.

Source

Thrown at source/index.ts:1562

		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));
	}
}

export function assertEmptyString(value: unknown, message?: string): asserts value is '' {
	if (!isEmptyString(value)) {
		throw new TypeError(message ?? typeErrorMessage('empty string', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Ensure the value is an actual `Map` instance, not a plain object — construct with `new Map()`
  2. If the Map may legitimately have entries, use `assert.map(value)` (non-empty allowed) or `is.emptyMap(value)` as a boolean check instead of asserting
  3. Clear the Map (`map.clear()`) before the assertion if empty state is required
  4. Pass a custom `message` argument to make the failure context clearer

Example fix

// before
assert.emptyMap({}); // plain object, throws
// after
assert.emptyMap(new Map());
Defensive patterns

Strategy: type-guard

Validate before calling

if (is.map(value) && value.size === 0) {
  assert.emptyMap(value); // safe
}

Type guard

function isEmptyMap(value: unknown): value is Map<unknown, unknown> {
  return value instanceof Map && value.size === 0;
}

Try / catch

try {
  assert.emptyMap(value);
} catch (error) {
  if (error instanceof TypeError) {
    // value was not an empty Map — inspect is(value) in the message
  } else throw error;
}

Prevention

When it happens

Trigger: Calling `assert.emptyMap(value)` / `assertEmptyMap(value)` with a non-Map value (object, array, null), or with a Map that has one or more entries.

Common situations: Validating that a cache/registry starts empty before initialization; test setup asserting a Map was cleared; passing a plain object `{}` where a real `Map` instance is expected; a Map unexpectedly retaining entries because `clear()` was never called or state leaked between tests.

Related errors


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