JS Built-ins
TypeScriptintermediate

Advanced β€” filterNullish

πŸ“– Usage Example
filterNullish([1, null, 2, undefined, 3])   // β†’ [1, 2, 3]

Using a type predicate filter removes null and undefined, narrowing the array element type.

Advanced Patterns: filterNullish

Challenge: Implement filterNullish<T>(arr: (T | null | undefined)[]): T[] returning only non-null, non-undefined values.

filterNullish([1, null, 2, undefined, 3])  // β†’ [1, 2, 3]
πŸ’‘ Hints (1)
  • β€’ A type predicate (x): x is T allows TypeScript to narrow the return type.