sindresorhus/is · error · TypeError

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

Error message

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

What it means

Thrown by `assertDate()` when the value fails `isDate` (object type 'Date'). It guarantees Date-instance methods are available; note it does NOT reject invalid dates (`new Date('garbage')` still passes — that's `assert.validDate`).

Source

Thrown at source/index.ts:1544

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

export function assertClass<T>(value: unknown, message?: string): asserts value is Class<T> {
	if (!isClass(value)) {
		throw new TypeError(message ?? typeErrorMessage('Class', value));
	}
}

export function assertDataView(value: unknown, message?: string): asserts value is DataView {
	if (!isDataView(value)) {
		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));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Convert at the boundary: `new Date(isoStringOrTimestamp)` before asserting.
  2. For date-library objects, call their native conversion (`m.toDate()`, `dt.toJSDate()`).
  3. If invalid dates must also be rejected, use `assert.validDate` instead.
  4. Add a JSON reviver or DTO mapping layer so dates are revived once, centrally.

Example fix

// before
assert.date(apiResponse.createdAt);
// after
assert.date(new Date(apiResponse.createdAt));
Defensive patterns

Strategy: validation

Validate before calling

const date = value instanceof Date ? value : new Date(value);
if (Number.isNaN(date.getTime())) {
  throw new Error(`Unparseable date input: ${String(value)}`);
}

Type guard

function isDate(value: unknown): value is Date {
  return value instanceof Date;
}

Try / catch

try {
  assert.date(value);
} catch (error) {
  if (error instanceof TypeError) {
    // ISO string or epoch number slipped through — parse to Date at the deserialization boundary
  } else throw error;
}

Prevention

When it happens

Trigger: Calling `assert.date(value)` with an ISO date string, a Unix timestamp number, a Moment/Dayjs/Luxon object, or a date that went through JSON serialization (JSON turns Dates into strings).

Common situations: API responses and database JSON columns deliver dates as ISO strings; `JSON.parse` never revives Dates; ORMs configured to return strings; timestamps stored as epoch numbers; date-library wrapper objects passed where native Date is required.

Related errors


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