-
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
Type is not inferred correctly using filter operator #3125
Comments
The problem is that the function you are passing to filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd) |
Thanks for your comment, but... how the I tested your proposed solution:
So, I removed the
... and it does change nothing at all, the type is still incorrectly inferred. Btw, the solutions I came up are: 1 - Cast it:
2 - Crazy option -> Don't use
|
I briefly looked at the Angular docs and those that I saw suggested the filter((e: RouterEvent): e is NavigationEnd => e instanceof NavigationEnd) The key point is that this is not an RxJS issue; it's a TypeScript issue. The function has to be explicitly made a type guard, it won't be inferred as one just because it happens to return the result of an |
Strange. How is it a typescript issue if I can get the correct type with the following code (using type guard also)?
|
@rafaelss95 To complete @cartant's answer, you can read more in TypeScript's documentation about the so-called Type Guards. Note the use of the special return type for the function passed to |
Because this: if (e instanceof NavigationEnd) { is a type guard, but this: e => e instanceof NavigationEnd is not a user-defined type guard. However, this: (e: RouterEvent): e is NavigationEnd => e instanceof NavigationEnd is a user-defined type guard. |
Ok, I got it. Now the question is: does it works as intended or its a bug? |
I think rxjs can't do more than what TypeScript is capable. So you can consider that it is as intended. :) |
I just enabled
Workaround:
|
This is not an rxjs issue. Related upstream issues are microsoft/TypeScript#5101 and microsoft/TypeScript#10734. |
I'm going to close issue as it's sufficiently explained for reason, and it's upstream issue that we can't make action at this moment until we get compiler support. |
any idea how to hotfix this for cc @cartant |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
RxJS version:
5.5.2
Code to reproduce:
Consider this piece of code:
Expected behavior:
I'd expect
e
to be inferred asNavigationEnd
.Actual behavior:
I'm using Angular 5 and in code above, I'm using
filter
operator to "listen" only events referent toNavigationEnd
, however the type isn't being inferred correctly. Instead of gettinge
asNavigationEnd
, I'm gettinge
asRouterEvent | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd | ActivationStart | ActivationEnd
.Additional information:
DEMO
The text was updated successfully, but these errors were encountered: