Skip to content
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

Merged
merged 1 commit into from
Oct 20, 2020

Conversation

josepot
Copy link
Contributor

@josepot josepot commented Oct 15, 2020

I think that the changes on this PR improve the types of the BooleanConstructor overload of filter

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! 😬

@josepot josepot force-pushed the feat/filter-boolean-type branch 2 times, most recently from 00b4f23 to ad951b1 Compare October 15, 2020 22:15
Copy link
Collaborator

@cartant cartant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@cartant cartant left a 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:

// 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>>;

@josepot
Copy link
Contributor Author

josepot commented Oct 15, 2020

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)

I was under the impression that the current dtslint test already does that.

spec-dtslint/operators/filter-spec.ts Outdated Show resolved Hide resolved
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>;
Copy link
Member

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

@benlesh benlesh merged commit d2658fa into ReactiveX:master Oct 20, 2020
@josepot josepot deleted the feat/filter-boolean-type branch October 20, 2020 18:59
@benlesh
Copy link
Member

benlesh commented Oct 20, 2020

OH NO! haha... This fails and worse, it's typed as Observable<true>... and it's obvious why once you think about it.

  const x = of(false, false, false, false).pipe(filter(Boolean)); // $ExpectType Observable<never>

I'm trying to work on an improvement right now.

@josepot
Copy link
Contributor Author

josepot commented Oct 20, 2020

right, because false type is boolean, if it had been of(false as const).pipe(filter(Boolean)) then the expected type would have been Observable<never>... I don't know, I won't feel bad if you revert the commit, but IMO that kinda makes sense 🤷

@benlesh
Copy link
Member

benlesh commented Oct 20, 2020

I'm still thinking about this, really.

@benlesh
Copy link
Member

benlesh commented Oct 20, 2020

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 extends false. I'm sure there's some principled reason for this, but it's baffling to me at the moment.

@josepot
Copy link
Contributor Author

josepot commented Oct 20, 2020

Yup, fair enough. I agree. Please revert and let's pretend that this never happened 😅 Thanks!

@benlesh
Copy link
Member

benlesh commented Oct 20, 2020

@josepot Naw.. it's a good change... and it spurred me to do this:

#5842

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants