Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

Why `filter(Boolean)` Is Not a Nullish Filter in TypeScript

`filter(Boolean)` keeps truthy values, not merely non-nullish ones. Use an explicit nullish check when valid data can include 0, false, or an empty string.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

filter(Boolean) removes more than null and undefined: it also drops 0, false, NaN, 0n, and the empty string. If those values are valid data, use values.filter(value => value != null) to remove only nullish entries.

Use a nullish check when you mean “not null or undefined”

For an array where zero, false, or an empty string must be preserved, filter with an explicit nullish condition:

As an Amazon Associate I earn from qualifying purchases.

const present = values.filter(value => value != null);

In JavaScript and TypeScript, value != null matches both null and undefined. If your codebase avoids loose equality, the equivalent explicit form is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const present = values.filter(
  value => value !== null && value !== undefined
);

For reuse, define a type predicate whose condition exactly matches its type claim:

function isNonNullish<T>(value: T): value is NonNullable<T> {
  return value !== null && value !== undefined;
}

const present = values.filter(isNonNullish);

What filter(Boolean) actually removes

Array.prototype.filter calls its callback for each element and keeps the element when the callback result is truthy. Passing Boolean directly converts each element to a boolean; it is a truthiness test, not a nullish test. JavaScript falsy values include:

  • false
  • 0 and -0
  • 0n
  • NaN
  • "" (the empty string)
  • null
  • undefined

Thus, a truthiness filter is appropriate only when every falsy value should be discarded. The MDN Boolean reference documents the conversion behavior and falsy values.

Rank #2
TypeScript Programming Language - Software Engineer & Coder T-Shirt
  • TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
  • TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem

Why the distinction matters for TypeScript arrays

Consider counts where zero is a meaningful value:

const counts: Array<number | null | undefined> = [0, 1, null, undefined];

const truthy = counts.filter(Boolean); // runtime result: [1]
const nonNullish = counts.filter(value => value != null); // [0, 1]

The first filter silently loses the valid count 0. The same problem arises if an array’s domain treats false or "" as meaningful. Choose the predicate according to what the data means, rather than using truthiness as shorthand for “present.”

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For object-only arrays whose optional members are null or undefined, truthiness can be an effective runtime filter: ordinary objects are truthy. But an explicit nullish check states the intended exclusion more precisely and remains correct if the element type later includes meaningful primitive values.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What TypeScript infers depends on the check and version

Runtime filtering and static type narrowing are related but distinct. JavaScript decides which elements survive based on the callback’s result. TypeScript analyzes callback forms under its type-predicate inference rules; a boolean-returning callback is not automatically a type guard in every case.

TypeScript 5.5 introduced inferred type predicates for suitable functions, and its release notes demonstrate filtering an optional value with .filter(score => score !== undefined). They also explain the scope of truthiness inference: “A truthiness check will infer a type predicate for object types, where there’s no ambiguity.” That object-specific qualification does not make a truthiness check a general-purpose nullish filter for primitive unions. See the TypeScript 5.5 release notes for the version-specific behavior.

For broader guidance on truthiness narrowing and explicit null checks, consult the official TypeScript Handbook section on narrowing.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.