sindresorhus/is · error · TypeError

Expected value which is `empty object`, received value of ty

Error message

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

What it means

Thrown by `assertEmptyObject` (source/index.ts:1566) when the value is not a plain object with zero enumerable own keys. The library checks both that the value is a plain object and that `Object.keys(value).length === 0`; anything else — a non-object, a class instance, or an object with properties — fails and throws a TypeError with the detected type from `is(value)`.

Source

Thrown at source/index.ts:1568

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

export function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is '' | Whitespace {
	if (!isEmptyStringOrWhitespace(value)) {
		throw new TypeError(message ?? typeErrorMessage('empty string or whitespace', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Pass a plain `{}` object — arrays, null, and class instances are rejected regardless of contents
  2. If keys are expected, use `assert.nonEmptyObject(value)` or `assert.plainObject(value)` instead
  3. Delete or omit stray properties before the assertion if empty state is the invariant
  4. Use the boolean form `is.emptyObject(value)` when failure should not throw

Example fix

// before
assert.emptyObject({debug: false}); // has a key, throws
// after
assert.emptyObject({});
Defensive patterns

Strategy: type-guard

Validate before calling

if (is.plainObject(value) && Object.keys(value).length === 0) {
  assert.emptyObject(value);
}

Type guard

function isEmptyObject(value: unknown): value is Record<string, never> {
  return is.plainObject(value) && Object.keys(value).length === 0;
}

Try / catch

try {
  assert.emptyObject(value);
} catch (error) {
  if (error instanceof TypeError) {
    // not an empty plain object — arrays, Maps, class instances all fail here
  } else throw error;
}

Prevention

When it happens

Trigger: Calling `assert.emptyObject(value)` with an object that has at least one enumerable key, with an array, null, a class instance, or any non-plain-object value.

Common situations: Asserting an options/config object was left empty; verifying an API response body is `{}`; surprises from objects with only symbol keys or non-enumerable properties (which still count as empty since only enumerable string keys are checked); accidentally passing `[]` or `null` instead of `{}`.

Related errors


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