sindresorhus/is · error · TypeError

Expected value which is `non-empty string and not whitespace

Error message

Expected value which is `non-empty string and not whitespace`, received value of type `${is(value)}`.

What it means

Thrown by `assertNonEmptyStringAndNotWhitespace()` (source/index.ts:1771) when the value is not a string containing at least one non-whitespace character. It is the stricter sibling of `assertNonEmptyString`: `''`, `' '`, `'\t\n'`, and all non-strings fail.

Source

Thrown at source/index.ts:1773

		throw new TypeError(message ?? typeErrorMessage('non-empty object', value));
	}
}

export function assertNonEmptySet<T = unknown>(value: unknown, message?: string): asserts value is Set<T> {
	if (!isNonEmptySet(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-empty set', value));
	}
}

export function assertNonEmptyString(value: unknown, message?: string): asserts value is string {
	if (!isNonEmptyString(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-empty string', value));
	}
}

export function assertNonEmptyStringAndNotWhitespace(value: unknown, message?: string): asserts value is string {
	if (!isNonEmptyStringAndNotWhitespace(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-empty string and not whitespace', value));
	}
}

export function assertNonNegativeInteger(value: unknown, message?: string): asserts value is number {
	if (!isNonNegativeInteger(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-negative integer', value));
	}
}

export function assertNonNegativeNumber(value: unknown, message?: string): asserts value is number {
	if (!isNonNegativeNumber(value)) {
		throw new TypeError(message ?? typeErrorMessage('non-negative number', value));
	}
}

// eslint-disable-next-line @typescript-eslint/no-restricted-types
export function assertNull(value: unknown, message?: string): asserts value is null {
	if (!isNull(value)) {

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Trim input at the boundary (`value.trim()`) and validate the trimmed result — then use the trimmed value downstream.
  2. Verify the config/env source actually contains a real value, not padding or a blank line.
  3. If blank input is a legal 'skip' signal, branch on `is.nonEmptyStringAndNotWhitespace(value)` instead of asserting.

Example fix

// before
assert.nonEmptyStringAndNotWhitespace(userInput); // throws on '   '
// after
const name = userInput.trim();
assert.nonEmptyStringAndNotWhitespace(name);
save(name);
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof value === 'string' && value.trim().length > 0) {
  assert.nonEmptyStringAndNotWhitespace(value);
}

Type guard

function isNonEmptyStringAndNotWhitespace(v: unknown): v is string {
  return typeof v === 'string' && v.trim().length > 0;
}

Try / catch

try {
  assert.nonEmptyStringAndNotWhitespace(title);
} catch (error) {
  if (error instanceof TypeError) {
    // title is empty or whitespace-only (e.g. '   ') — reject or re-prompt for input
  }
  throw error;
}

Prevention

When it happens

Trigger: Calling `assert.nonEmptyStringAndNotWhitespace(value)` with a whitespace-only string (user typed spaces in a form, a padded CSV/env value), the empty string, or a non-string value.

Common situations: Untrimmed user input from forms or CLI prompts; config values accidentally saved as blank lines or spaces; copy-pasted secrets/tokens that captured only whitespace.

Related errors


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