guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
42 lines (41 loc) • 1.64 kB
TypeScript
import type { TypeGuardFn } from './isType';
import type { BooleanLike } from '../types/BooleanLike';
/**
* Checks if a value can be treated as a boolean.
*
* This type guard validates that a value is either a boolean or can be converted to a boolean.
* It accepts boolean values, string representations ("true", "false", "1", "0"), and numeric
* representations (1, 0). This is useful for validating boolean values from various sources
* like form inputs, API responses, configuration files, etc.
*
* @param value - The value to check
* @param config - Optional configuration for error handling
* @returns True if the value is a boolean or can be converted to a boolean, false otherwise
*
* @example
* ```typescript
* import { isBooleanLike } from 'guardz';
*
* console.log(isBooleanLike(true)); // true
* console.log(isBooleanLike(false)); // true
* console.log(isBooleanLike("true")); // true
* console.log(isBooleanLike("false")); // true
* console.log(isBooleanLike("1")); // true
* console.log(isBooleanLike("0")); // true
* console.log(isBooleanLike(1)); // true
* console.log(isBooleanLike(0)); // true
* console.log(isBooleanLike("yes")); // false
* console.log(isBooleanLike("no")); // false
* console.log(isBooleanLike("")); // false
* console.log(isBooleanLike(null)); // false
* console.log(isBooleanLike(undefined)); // false
*
* // With type narrowing
* const data: unknown = getDataFromAPI();
* if (isBooleanLike(data)) {
* // data is now typed as BooleanLike
* console.log(Boolean(data)); // Safe to convert
* }
* ```
*/
export declare const isBooleanLike: TypeGuardFn<BooleanLike>;