TypeScript-safe truthiness filters: remove null/undefined without dropping 0 or ''
filter(Boolean) is short and tempting, but it removes every falsy value — including 0, '', false, and NaN. When arrays can contain those values, that shorthand silently changes program meaning. The safer approach is a tiny explicit predicate that removes only null and undefined and a small wrapper that makes the contract obvious. What the verified example does (no code quoted here): isNonNullish: a simple predicate that returns true for any value that is not null or undefined. filterNullish: accepts an array, throws a TypeError if the input is not an array, and returns a new array with only nu
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in