Skip to content

isTruthy

Checks if a value is truthy (not false, null, undefined, 0, "", or NaN).

This is the type-safe alternative to Boolean() as a filter callback. Unlike filter(Boolean), using filter(isTruthy) correctly narrows the resulting array type by excluding falsy values.

Available since v2.0.0

import { isTruthy } from '@helpers4/type';
isTruthy<T>(value: Falsy | T): value is T
ParameterTypeDescription
valueFalsy | TThe value to check

value is T — True if the value is truthy

Returns true for all truthy values, false for falsy ones.

isTruthy(1)         // => true
isTruthy('hello')   // => true
isTruthy(0)         // => false
isTruthy(null)      // => false

Use isTruthy with Array.filter to get correct TypeScript narrowing.

const items = ['a', '', null, 'b', undefined];
const result = items.filter(isTruthy);
// => ['a', 'b'] with type string[]

View source on GitHub