sindresorhus/is · error · TypeError

Expected value which is `Object`, received value of type `${

Error message

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

What it means

Thrown by `assert.object()` when the value is not of type `object` per `isObject` — which accepts plain objects, arrays, class instances, and functions, but rejects primitives and null. It exists so downstream code can safely access properties after the TypeScript narrowing `asserts value is object`.

Source

Thrown at source/index.ts:1818

	}
}

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

export function assertNumericString(value: unknown, message?: string): asserts value is `${number}` {
	if (!isNumericString(value)) {
		throw new TypeError(message ?? typeErrorMessage('string with a number', value));
	}
}

// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertObject(value: unknown, message?: string): asserts value is object {
	if (!isObject(value)) {
		throw new TypeError(message ?? typeErrorMessage('Object', value));
	}
}

export function assertObservable(value: unknown, message?: string): asserts value is ObservableLike {
	if (!isObservable(value)) {
		throw new TypeError(message ?? typeErrorMessage('Observable', value));
	}
}

export function assertOddInteger(value: number, message?: string): asserts value is number {
	if (!isOddInteger(value)) {
		throw new TypeError(message ?? typeErrorMessage('odd integer', value));
	}
}

export function assertPlainObject<Value = unknown>(value: unknown, message?: string): asserts value is Record<PropertyKey, Value> {
	if (!isPlainObject(value)) {
		throw new TypeError(message ?? typeErrorMessage('plain object', value));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Check for null/undefined before asserting: the value is likely absent, not mistyped — fix the producer to always return an object or handle the missing case.
  2. If you specifically need a plain object (not arrays/functions), use `assert.plainObject()` for a stricter check.
  3. Inspect the reported received type in the message to locate which caller passed a primitive.
  4. Validate JSON payloads at the trust boundary with a schema before deeper assertions.

Example fix

// before
const config = JSON.parse(raw); // raw = 'null'
assert.object(config); // throws: received 'null'
// after
const config = JSON.parse(raw) ?? {};
assert.object(config);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof value !== 'object' || value === null) {
  throw new TypeError(`Expected object, got ${value === null ? 'null' : typeof value}`);
}

Type guard

function isObject(value: unknown): value is object {
  return (typeof value === 'object' && value !== null) || typeof value === 'function';
}

Try / catch

try {
  ow(value, ow.object);
} catch (error) {
  if (error instanceof ArgumentError) {
    throw new TypeError(`Expected an object argument: ${error.message}`);
  }
  throw error;
}

Prevention

When it happens

Trigger: Calling `assert.object(value)` with null (typeof null is 'object' but isObject excludes it), undefined, a string, number, boolean, or symbol. Frequently hit when JSON.parse returns a scalar or when an optional field is absent.

Common situations: Deserialized API responses where the body is null or a bare string instead of an object; accessing a missing nested config key and asserting on undefined; a function returning null on failure instead of throwing.

Related errors


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