sindresorhus/is · error · TypeError

Expected value which is `non-empty string`, received value o

Error message

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

What it means

Thrown by `assertNonEmptyString()` (source/index.ts:1765) when the value is not a primitive string with `length > 0`. Both the empty string `''` and non-strings (numbers, `null`, `undefined`, String objects) fail. Note that whitespace-only strings like `' '` pass this check.

Source

Thrown at source/index.ts:1767

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

export function assertNonEmptyObject<Key extends keyof any = string, Value = unknown>(value: unknown, message?: string): asserts value is Record<Key, Value> {
	if (!isNonEmptyObject(value)) {
		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));

View on GitHub ↗ (pinned to 7821031c66)

Solutions

  1. Check the value's source — for env vars, confirm the variable is set and non-empty in the running environment.
  2. Coerce intentionally if appropriate: `String(id)` for numbers.
  3. If empty means 'not provided' and that's valid, guard with `is.nonEmptyString(value)` and handle the absent case explicitly.
  4. If whitespace-only input must also be rejected, use `assert.nonEmptyStringAndNotWhitespace` instead.

Example fix

// before
const apiUrl = process.env.API_URL;
assert.nonEmptyString(apiUrl); // throws: received `undefined`
// after
const apiUrl = process.env.API_URL;
if (!is.nonEmptyString(apiUrl)) {
  throw new Error('API_URL environment variable must be set to a non-empty URL');
}
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Try / catch

try {
  assert.nonEmptyString(name);
} catch (error) {
  if (error instanceof TypeError) {
    // name is '', null, undefined, or non-string — reject the input with a clear message
  }
  throw error;
}

Prevention

When it happens

Trigger: Calling `assert.nonEmptyString(value)` with `''`, `undefined`/`null` (unset env var or missing property), a number (e.g. an ID that should have been stringified), or `new String('x')` (object wrapper).

Common situations: Missing environment variables (`process.env.FOO` is `undefined` or set to empty in .env/CI); empty form inputs; optional API fields defaulting to `''`; numeric IDs passed where a string is expected.

Related errors


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