-
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
Proposal: filter and partition as typescript typeguard #2340
Comments
Unfortunately, as far as I know typescript's inference doesn't work automatically in this case and you may need supply generic types explicitly to narrow down types as you'd like to have. Current type definition for export function typeGuard(root: HTMLElement) {
return Observable.fromEvent<Event>(root, 'click', {capture: true})
.filter<Event, MouseEvent>((value: Event): value is MouseEvent => value instanceof MouseEvent)
.do(function (e) {
e.clientX; //now chained operator got narrowed down type <S> instead of original type <T>
});
} . I'll leave this opened bit more and /cc @david-driscoll just in case if there's any possible improvement. |
@Bnaya I think @kwonoj is correct here. While If you make the method explicitly a typeguard it should work. I don't think the concept of implicit typeguard exists in TypeScript (I could be wrong). In order for this to work any other way would most likely require a compiler change which would "promote" the inner typeguard (collection of type guards creating a union type??) to the parent method. |
Thanks! |
@kwonoj do you have a suggestion how to handle |
filter and partition as typescript typeguard
When filtering or partitioning stream or events,
And the filter criteria will narrow down possible types,
the partition / filter returned Observable type should be narrowed down as-well
Given the following code:
The
e
parameter ofdo
should be of typeMouseEvent
How to implement:
Generics!
And maybe is possible with some inference magic
The text was updated successfully, but these errors were encountered: