isBoolean function
Checks whether a value is a boolean.
ts
import { isBoolean } from 'utils';Signature
ts
export function isBoolean(value: unknown): value is booleanParameters
| Name | Description |
|---|---|
value | The value to check. |
Returns
true for true and false only (not for truthy or falsy values).
Example
ts
isBoolean(false); // true
isBoolean(0); // falseTests
Line coverage: 100 %.
- ✅ accepts booleans only
Source
src/guard/is-boolean.ts (on GitHub)
Source code
ts
/**
* Checks whether a value is a boolean.
*
* @param value - The value to check.
* @returns `true` for `true` and `false` only (not for truthy or falsy values).
* @example
* isBoolean(false); // true
* isBoolean(0); // false
*/
export function isBoolean(value: unknown): value is boolean {
return typeof value === 'boolean';
}Test file
ts
import { isBoolean } from './is-boolean';
describe(isBoolean, () => {
it('accepts booleans only', () => {
expect(isBoolean(false)).toBe(true);
expect(isBoolean(0)).toBe(false);
expect(isBoolean('true')).toBe(false);
});
});Keywords: is, boolean, guards, function