-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(filter): improve type inference for filter(Boolean) #5831
Conversation
00b4f23
to
ad951b1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ad951b1
to
2230fda
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at this again, I think a dtstlint test needs to be added to ensure that the problem mentioned in this comment does not occur: #4959 (comment)
See this comment which refers to the type signature that this PR changes:
rxjs/src/internal/operators/filter.ts
Lines 8 to 9 in adbe65e
// NOTE(benlesh): T|null|undefined solves the issue discussed here: https://github.com/ReactiveX/rxjs/issues/4959#issuecomment-520629091 | |
export function filter<T>(predicate: BooleanConstructor): OperatorFunction<T | null | undefined, NonNullable<T>>; |
I was under the impression that the current dtslint test already does that. |
2230fda
to
051ad34
Compare
export function filter<T>(predicate: BooleanConstructor): OperatorFunction<T | null | undefined, NonNullable<T>>; | ||
export function filter<T>( | ||
predicate: BooleanConstructor | ||
): OperatorFunction<T, T extends null | undefined | false | 0 | -0 | 0n | '' ? never : T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this is something that must exist elsewhere. first()
for example. We probably need a type like:
type Truthy<T> = T extends null | undefined | false | 0 | -0 | 0n | '' ? never : T
OH NO! haha... This fails and worse, it's typed as const x = of(false, false, false, false).pipe(filter(Boolean)); // $ExpectType Observable<never> I'm trying to work on an improvement right now. |
right, because |
I'm still thinking about this, really. |
I mean... look at how bananas this is: type Test<T> = T extends true ? 'T' : 'F';
function test<T>(value: readonly T[]): Test<T> {
return null!;
}
const o = test([false, false]); // $ExpectType "F" | "T" (no, really). Same for |
Yup, fair enough. I agree. Please revert and let's pretend that this never happened 😅 Thanks! |
I think that the changes on this PR improve the types of the
BooleanConstructor
overload offilter
EDIT: I force-pushed after @cartant approved the PR because I realized that I forgot to add
-0
to the list of falsy types. Sorry about that! 😬