guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
39 lines (38 loc) • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isBoolean = void 0;
const reportTypeGuardError_1 = require("../utils/reportTypeGuardError");
/**
* Checks if a value is a boolean.
*
* @param value - The value to check
* @param config - Optional configuration for error handling
* @returns True if the value is a boolean, false otherwise
*
* @example
* ```typescript
* import { isBoolean } from 'guardz';
*
* console.log(isBoolean(true)); // true
* console.log(isBoolean(false)); // true
* console.log(isBoolean(1)); // false
* console.log(isBoolean(0)); // false
* console.log(isBoolean("true")); // false
* console.log(isBoolean(null)); // false
*
* // With type narrowing
* const data: unknown = getUserInput();
* if (isBoolean(data)) {
* // data is now typed as boolean
* console.log(data ? "yes" : "no");
* }
* ```
*/
const isBoolean = function isBooleanGuard(value, config) {
if (typeof value !== 'boolean') {
(0, reportTypeGuardError_1.reportTypeGuardError)(config, value, 'boolean');
return false;
}
return true;
};
exports.isBoolean = isBoolean;