Skip to content

prefer-nullish-filter-predicate

Prefer an explicit nullish predicate when filter(Boolean) removes only nullish values but does not narrow the result type.

Why

An explicit nullish predicate preserves the same runtime elements while letting TypeScript remove null and undefined from the result.

Fix

Replace filter(Boolean) with filter((value) => value !== null && value !== undefined).

Examples

Before — flagged Boolean filtering loses nullish narrowing
src/users.ts
declare const users: readonly ({ id: string } | null)[];
const present = users.filter(Boolean);
After — preferred Nullish filtering narrows the result
src/users.ts
declare const users: readonly ({ id: string } | null)[];
const present = users.filter((user) => user !== null && user !== undefined);