sindresorhus/is · error · TypeError
Expected value which is `empty string or whitespace`, receiv
Error message
Expected value which is `empty string or whitespace`, received value of type `${is(value)}`. What it means
Thrown by `assertEmptyStringOrWhitespace` (source/index.ts:1584) when the value is neither the empty string nor a string consisting only of whitespace characters. On success the type narrows to `'' | Whitespace`; any string with non-whitespace content, or any non-string, throws a TypeError with the detected type.
Source
Thrown at source/index.ts:1586
throw new TypeError(message ?? typeErrorMessage('empty object', value));
}
}
export function assertEmptySet(value: unknown, message?: string): asserts value is Set<never> {
if (!isEmptySet(value)) {
throw new TypeError(message ?? typeErrorMessage('empty set', value));
}
}
export function assertEmptyString(value: unknown, message?: string): asserts value is '' {
if (!isEmptyString(value)) {
throw new TypeError(message ?? typeErrorMessage('empty string', value));
}
}
export function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is '' | Whitespace {
if (!isEmptyStringOrWhitespace(value)) {
throw new TypeError(message ?? typeErrorMessage('empty string or whitespace', value));
}
}
export function assertEnumCase<T = unknown>(value: unknown, targetEnum: T, message?: string): asserts value is T[keyof T] {
if (!isEnumCase(value, targetEnum)) {
throw new TypeError(message ?? typeErrorMessage('EnumCase', value));
}
}
export function assertError(value: unknown, message?: string): asserts value is Error {
if (!isError(value)) {
throw new TypeError(message ?? typeErrorMessage('Error', value));
}
}
export function assertEvenInteger(value: number, message?: string): asserts value is number {
if (!isEvenInteger(value)) {
throw new TypeError(message ?? typeErrorMessage('even integer', value));View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Confirm the value is actually a string — null/undefined must be handled before the assertion
- If any content is legitimate, replace the assertion with the boolean `is.emptyStringOrWhitespace(value)` and branch
- Normalize the input (e.g. `value ?? ''`) upstream if blank should be represented as an empty string
Example fix
// before assert.emptyStringOrWhitespace(null); // non-string, throws // after assert.emptyStringOrWhitespace(input ?? '');
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof value === 'string' && value.trim().length === 0) {
assert.emptyStringOrWhitespace(value);
} Type guard
function isEmptyStringOrWhitespace(value: unknown): value is string {
return typeof value === 'string' && value.trim().length === 0;
} Try / catch
try {
assert.emptyStringOrWhitespace(value);
} catch (error) {
if (error instanceof TypeError) {
// value is non-string or contains non-whitespace characters
} else throw error;
} Prevention
- This check requires a string — null/undefined fail it, so guard with typeof first
- Use value.trim() === '' as the equivalent inline check
- Be aware which whitespace definition applies (String.prototype.trim covers Unicode whitespace)
When it happens
Trigger: Calling `assert.emptyStringOrWhitespace(value)` with a string containing any non-whitespace character, or with a non-string value (number, null, undefined).
Common situations: Validating that an optional text field was left blank; a value that looks blank but contains invisible non-whitespace characters (e.g. zero-width joiners not classified as whitespace); receiving `null` instead of `''` from a form library or database column.
Related errors
- Expected value which is `empty string`, received value of ty
- Expected value which is `whitespace string`, received value
- Expected value which is `empty map`, received value of type
- Expected value which is `empty object`, received value of ty
- Expected value which is `empty set`, received value of type
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/a6b7ae6256281500.json.
Report an issue: GitHub ↗.