constructor(private router: Router) { }
ngOnInit() {
this.router.events.pipe(
filter(e => e instanceof NavigationEnd)
).subscribe(
// e is inferred as RouterEvent | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd | ActivationStart | ActivationEnd
e => console.log(e)
);
In code above, I'm using filter operator to "listen" only events referent to NavigationEnd, however the type isn't being inferred correctly. Instead of getting e as NavigationEnd, I'm getting e as RouterEvent | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd | ActivationStart | ActivationEnd.
Current behavior
Consider this piece of code:
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
.Demo
https://stackblitz.com/edit/angular-gitter-pjkkjx
Environment
Angular version: 5.0.2 rxjs 5.5.2
Actually, to get the correct type inferred you have to put an
if
in your subscribe like that: