sindresorhus/is · error · TypeError
Expected value which is `boolean`, received value of type `$
Error message
Expected value which is `boolean`, received value of type `${is(value)}`. What it means
Thrown by `assertBoolean()` when the value is not a primitive `true`/`false` per `isBoolean` (typeof check; Boolean objects don't count in strict primitive terms). Ensures code branching on the value gets a genuine boolean rather than a truthy/falsy stand-in.
Source
Thrown at source/index.ts:1510
throw new TypeError(message ?? typeErrorMessage('BigInt64Array', value));
}
}
export function assertBigUint64Array(value: unknown, message?: string): asserts value is BigUint64Array {
if (!isBigUint64Array(value)) {
throw new TypeError(message ?? typeErrorMessage('BigUint64Array', value));
}
}
export function assertBlob(value: unknown, message?: string): asserts value is Blob {
if (!isBlob(value)) {
throw new TypeError(message ?? typeErrorMessage('Blob', value));
}
}
export function assertBoolean(value: unknown, message?: string): asserts value is boolean {
if (!isBoolean(value)) {
throw new TypeError(message ?? typeErrorMessage('boolean', value));
}
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
export function assertBoundFunction(value: unknown, message?: string): asserts value is Function {
if (!isBoundFunction(value)) {
throw new TypeError(message ?? typeErrorMessage('bound Function', value));
}
}
/**
Note: [Prefer using `Uint8Array` instead of `Buffer`.](https://sindresorhus.com/blog/goodbye-nodejs-buffer)
*/
export function assertBuffer(value: unknown, message?: string): asserts value is NodeBuffer {
if (!isBuffer(value)) {
throw new TypeError(message ?? typeErrorMessage('Buffer', value));
}
}View on GitHub ↗ (pinned to 7821031c66)
Solutions
- Coerce string flags explicitly before asserting: `value === 'true'` for env vars/query params.
- Default optional flags: `assert.boolean(options.flag ?? false)` or validate presence separately.
- Fix the config source to use unquoted JSON booleans.
- For DB values, map 0/1 to Boolean at the data-access layer.
Example fix
// before assert.boolean(process.env.DEBUG); // after assert.boolean(process.env.DEBUG === 'true');
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof value !== 'boolean') {
// coerce deliberately: value = value === 'true' — never rely on truthiness
} Type guard
function isBoolean(value: unknown): value is boolean {
return typeof value === 'boolean';
} Try / catch
try {
assert.boolean(value);
} catch (error) {
if (error instanceof TypeError) {
// strings 'true'/'false', 0/1, or undefined slipped through — coerce explicitly at the boundary
} else throw error;
} Prevention
- Environment variables and query params are always strings — parse 'true'/'false' explicitly before passing them on
- Avoid Boolean object wrappers (new Boolean(true)); they fail typeof checks
- Give optional boolean parameters explicit defaults (flag = false) so undefined never reaches the assertion
When it happens
Trigger: Calling `assert.boolean(value)` with the strings 'true'/'false', 0/1, undefined, null, or a `new Boolean(...)` wrapper object.
Common situations: Environment variables and query-string parameters are always strings ('true' !== true); JSON configs hand-edited with quoted booleans; database drivers returning 0/1 tinyint for boolean columns; optional flags left undefined instead of defaulted.
Related errors
- Expected value which is `Array`, received value of type `${i
- Expected value which is `ArrayBuffer`, received value of typ
- Expected value which is `array-like`, received value of type
- Expected value which is `AsyncFunction`, received value of t
- Expected value which is `AsyncGenerator`, received value of
AI-assisted analysis of sindresorhus/is@7821031c66 (2026-07-31).
Data as JSON: /data/errors/638d76e7de499b99.json.
Report an issue: GitHub ↗.