Skip to content

Commit 648f7c6

Browse files
authored
fix(filter-nil): improve type guard for filtering null and undefined values from enums (#660)
1 parent d5bcae1 commit 648f7c6

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { filter } from 'rxjs';
22

3+
// TypeScript's built-in `NonNullable` doesn't distribute properly over generic type parameters
4+
// in type predicate contexts, causing the type guard to fail. This custom conditional type
5+
// forces distribution by being evaluated directly in the generic context.
6+
type NotNullable<T> = T extends null | undefined ? never : T;
7+
38
export const filterNil = <T>() =>
49
filter(
5-
(value: T): value is NonNullable<T> =>
10+
(value: T): value is NotNullable<T> =>
611
value !== undefined && value !== null,
712
);

0 commit comments

Comments
 (0)