sindresorhus/is · error · TypeError

Expected value which is `string with a number`, received val

Error message

Expected value which is `string with a number`, received value of type `${is(value)}`.

What it means

Thrown by `assert.numericString()` when the value is not a string containing a valid number. `isNumericString` requires the value to be a string whose contents parse as a number (template-literal type `${number}`); non-strings and non-numeric strings both fail. Actual numbers also fail — the check is specifically for the string representation.

Source

Thrown at source/index.ts:1811

	}
}

// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertNullOrUndefined(value: unknown, message?: string): asserts value is null | undefined {
	if (!isNullOrUndefined(value)) {
		throw new TypeError(message ?? typeErrorMessage('null or undefined', value));
	}
}

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)) {

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. If the value is already a number, use `assert.number()` instead — numericString requires a string.
  2. Sanitize the input first: `value.trim()` and strip formatting characters before asserting.
  3. Trace where the empty/malformed string originates (missing query param defaults to '') and fail earlier with a clearer message.
  4. Pass a custom message as the second argument to identify which field failed.

Example fix

// before
assert.numericString(42); // throws: received 'number'
// after
assert.numericString('42');
// or, for numbers:
assert.number(42);
Defensive patterns

Strategy: validation

Validate before calling

if (typeof value !== 'string' || value.trim() === '' || Number.isNaN(Number(value))) {
  throw new TypeError(`Expected numeric string, got ${JSON.stringify(value)}`);
}

Type guard

function isNumericString(value: unknown): value is string {
  return typeof value === 'string' && value.trim() !== '' && !Number.isNaN(Number(value));
}

Try / catch

try {
  ow(value, ow.string.numeric);
} catch (error) {
  if (error instanceof ArgumentError) {
    throw new TypeError(`Expected a string containing a number: ${error.message}`);
  }
  throw error;
}

Prevention

When it happens

Trigger: Calling `assert.numericString(value)` with an actual number (e.g. 42), an empty string, a string with units ('42px'), whitespace-padded or comma-formatted numbers ('1,000'), null, or undefined.

Common situations: Validating URL path/query parameters or HTTP headers expected to carry numeric IDs; CSV cell values with stray whitespace or thousands separators; passing an already-parsed number where the raw string was expected.

Related errors


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