Skip to content

isBoolean function ​

Checks whether a value is a boolean.

ts
import { isBoolean } from 'utils';

Signature ​

ts
export function isBoolean(value: unknown): value is boolean

Parameters ​

NameDescription
valueThe value to check.

Returns ​

true for true and false only (not for truthy or falsy values).

Example ​

ts
isBoolean(false); // true
isBoolean(0); // false

Tests ​

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

Generated from the JSDoc of the sources.