sindresorhus/is · error · TypeError
Expected value which is `empty string`, received value of ty
Error message
Expected value which is `empty string`, received value of type `${is(value)}`. What it means
Thrown by `assertEmptyString` (source/index.ts:1578) when the value is not exactly the empty string `''`. On success TypeScript narrows the value to the literal type `''`; any non-string, or a string with length > 0 (including whitespace-only strings), throws a TypeError reporting the detected type.
Source
Thrown at source/index.ts:1580
throw new TypeError(message ?? typeErrorMessage('empty map', value));
}
}
export function assertEmptyObject<Key extends keyof any = string>(value: unknown, message?: string): asserts value is Record<Key, never> {
if (!isEmptyObject(value)) {
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));View on GitHub ↗ (pinned to 7821031c66)
Solutions
- If whitespace-only strings should also pass, use `assert.emptyStringOrWhitespace(value)` instead
- Trim or reset the string to `''` before the assertion if empty is the required state
- Guard against null/undefined upstream — the assertion requires an actual string
- If the goal is the opposite (require content), use `assert.nonEmptyString(value)`
Example fix
// before
assert.emptyString(' '); // whitespace, throws
// after
assert.emptyStringOrWhitespace(' '); Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof value === 'string' && value.length === 0) {
assert.emptyString(value);
} Type guard
function isEmptyString(value: unknown): value is '' {
return typeof value === 'string' && value.length === 0;
} Try / catch
try {
assert.emptyString(value);
} catch (error) {
if (error instanceof TypeError) {
// value is non-string or a non-empty string (whitespace counts as non-empty here)
} else throw error;
} Prevention
- Remember ' ' (whitespace) is NOT an empty string for this check — use emptyStringOrWhitespace for that
- null and undefined are not empty strings; normalize with value ?? '' if that's the intent
- Prefer value === '' as a direct check when you don't need the assertion's throw behavior
When it happens
Trigger: Calling `assert.emptyString(value)` with a non-empty string, a whitespace-only string like `' '`, or a non-string value such as null or undefined.
Common situations: Asserting a form field or buffer was reset; whitespace-only input like `' '` unexpectedly failing because it isn't strictly empty; `undefined`/`null` from an unset env var or missing object property being passed where `''` was expected.
Related errors
- Expected value which is `empty string or whitespace`, receiv
- 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
- Expected value which is `EnumCase`, received value of type `
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/f0e2f13a2a63b977.json.
Report an issue: GitHub ↗.