sindresorhus/is · error · TypeError

Expected value which is `valid Date`, received value of type

Error message

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

What it means

Thrown by `assertValidDate()` in @sindresorhus/is when the value is not a Date instance whose time value is a real number — `isValidDate` requires `isDate(value) && !Number.isNaN(Number(value))`. It catches the classic 'Invalid Date' object, which IS a Date instance but represents no point in time.

Source

Thrown at source/index.ts:1975

	}
}

// eslint-disable-next-line unicorn/prevent-abbreviations
export function assertUrlSearchParams(value: unknown, message?: string): asserts value is URLSearchParams {
	if (!isUrlSearchParams(value)) {
		throw new TypeError(message ?? typeErrorMessage('URLSearchParams', value));
	}
}

export function assertUrlString(value: unknown, message?: string): asserts value is UrlString {
	if (!isUrlString(value)) {
		throw new TypeError(message ?? typeErrorMessage('string with a URL', value));
	}
}

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

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

// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertWeakMap<Key extends object = object, Value = unknown>(value: unknown, message?: string): asserts value is WeakMap<Key, Value> {
	if (!isWeakMap(value)) {
		throw new TypeError(message ?? typeErrorMessage('WeakMap', value));
	}
}

// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertWeakRef<T extends object = object>(value: unknown, message?: string): asserts value is WeakRef<T> {

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Validate/normalize input before constructing: parse known formats explicitly (ISO 8601 works everywhere: `new Date('2026-07-31')`) or use a date library for other formats.
  2. If you have a timestamp or string, convert first: `new Date(timestamp)` — the assertion only accepts Date objects.
  3. Trace where the Invalid Date was created — check the raw string that was fed to `new Date()`; it's usually an undefined or misformatted API field.

Example fix

// before
const d = new Date(user.dob); // '31/07/1990' → Invalid Date
assert.validDate(d);
// after
const [day, month, year] = user.dob.split('/').map(Number);
const d = new Date(year, month - 1, day);
assert.validDate(d);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

if (is.validDate(value)) {
  // value is a Date whose time is not NaN
}

Try / catch

try {
  assert.validDate(value);
} catch (error) {
  if (error instanceof TypeError) { /* Date exists but is Invalid Date */ }
  throw error;
}

Prevention

When it happens

Trigger: `assert.validDate(value)` with `new Date('not-a-date')` or any unparseable date string, a date string itself (strings always fail — only Date objects pass), a timestamp number, or null/undefined.

Common situations: Parsing user input or API date fields with `new Date(str)` where the string is empty, malformed, or in a locale format the engine rejects (e.g. 'DD/MM/YYYY'); undefined fields producing Invalid Date silently upstream.

Related errors


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