sindresorhus/is · warning · TypeError
Expected value which is `truthy`, received value of type `${
Error message
Expected value which is `truthy`, received value of type `${is(value)}`. What it means
Thrown by `assert.truthy()` (assertTruthy at source/index.ts:1906) when the value is falsy: `false`, `0`, `-0`, `0n`, `''`, `null`, `undefined`, or `NaN`. Unlike the other assertions this is not a type check but a value check — its assertion signature narrows `T | Falsy` to `T`, which is why the codebase uses it to strip null/undefined/empty from union types.
Source
Thrown at source/index.ts:1908
throw new TypeError(message ?? typeErrorMessage('SharedArrayBuffer', value));
}
}
export function assertString(value: unknown, message?: string): asserts value is string {
if (!isString(value)) {
throw new TypeError(message ?? typeErrorMessage('string', value));
}
}
export function assertSymbol(value: unknown, message?: string): asserts value is symbol {
if (!isSymbol(value)) {
throw new TypeError(message ?? typeErrorMessage('symbol', value));
}
}
export function assertTruthy<T>(value: T | Falsy, message?: string): asserts value is T {
if (!isTruthy(value)) {
throw new TypeError(message ?? typeErrorMessage('truthy', value));
}
}
export function assertTupleLike<T extends Array<TypeGuard<unknown>>>(value: unknown, guards: [...T], message?: string): asserts value is ResolveTypesOfTypeGuardsTuple<T> {
if (!isTupleLike(value, guards)) {
throw new TypeError(message ?? typeErrorMessage('tuple-like', value));
}
}
export function assertTypedArray(value: unknown, message?: string): asserts value is TypedArray {
if (!isTypedArray(value)) {
throw new TypeError(message ?? typeErrorMessage('TypedArray', value));
}
}
export function assertUint16Array(value: unknown, message?: string): asserts value is Uint16Array {
if (!isUint16Array(value)) {
throw new TypeError(message ?? typeErrorMessage('Uint16Array', value));View on GitHub ↗ (pinned to 7821031c66)
Solutions
- If `0` or `''` are legitimate values, replace with a specific check: `assert.number(value)` plus an explicit `!== undefined` guard, rather than truthiness.
- Trace why the value is `null`/`undefined` — a failed lookup or missing field — and handle that case explicitly before asserting.
- Provide a domain-specific `message` argument so failures identify which value was falsy.
Example fix
// before assert.truthy(items.indexOf(target)); // fails when index is 0! // after const index = items.indexOf(target); assert.truthy(index !== -1, 'target not found in items');
Defensive patterns
Strategy: validation
Validate before calling
if (!value) {
throw new TypeError(`Expected a truthy value, got ${value}`);
} Type guard
function isTruthy<T>(value: T): value is Exclude<T, false | 0 | 0n | '' | null | undefined> {
return Boolean(value);
} Try / catch
try {
assert.truthy(value);
} catch (error) {
if (error instanceof TypeError) {
// value was false, 0, -0, 0n, '', null, undefined, or NaN
}
throw error;
} Prevention
- Know the full falsy list: false, 0, -0, 0n, '', null, undefined, NaN — legitimate values like 0 and '' fail this check
- If 0 or empty string are valid inputs, use a null/undefined check (value != null) instead of a truthiness assertion
- Set defaults with ?? rather than || so falsy-but-valid values survive
When it happens
Trigger: Calling `assert.truthy(value)` where value is `undefined` (missing property/return), `null` (API no-result), `0` or `''` — the last two are frequent false positives when zero or empty string are legitimate values.
Common situations: Using truthiness as a null-check on numeric fields where `0` is valid (counts, indexes, prices) or string fields where `''` is valid; lookup functions (`map.get`, `find`) returning `undefined`; NaN from failed numeric parsing sneaking through.
Related errors
- Expected value which is `PropertyKey`, received value of typ
- Expected value which is `RegExp`, received value of type `${
- Expected value which is `safe integer`, received value of ty
- Expected value which is `string`, received value of type `${
- Expected value which is `symbol`, received value of type `${
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/7460784318d2ec98.json.
Report an issue: GitHub ↗.