Open9
filter(Boolean)


Here's what I don't like about this
const arr = [0, 1, "", "foo", null] as const;
const arr2 = arr.filter(Boolean);
// Error, good
if (arr2[0] === 3) {
}
// No error, bad
if (arr2[0] === 0) {
}
The PR is incomplete (in the technical sense of the word) the same way the current behavior is -- elements which are removed from the array are still represented as being possibly-there by the type system.
A non-coercing standalone function works well to eliminate surprising coercions
declare function isNotNull<T>(obj: T): obj is NonNullable<T>;
const arr = [0, 1, "", "foo", null] as const;
const arr2 = arr.filter(isNotNull);