Type guard for filtering out null and undefined without losing type precision.
null
undefined
Value that may be null or undefined.
true when value is neither null nor undefined; narrows the type to exclude both.
true
value
const bad = [1, null, 2, undefined, 3].filter(Boolean);// ^? (number | null | undefined)[]const good = [1, null, 2, undefined, 3].filter(isDefined);// ^? number[] Copy
const bad = [1, null, 2, undefined, 3].filter(Boolean);// ^? (number | null | undefined)[]const good = [1, null, 2, undefined, 3].filter(isDefined);// ^? number[]
Type guard for filtering out
nullandundefinedwithout losing type precision.