Reactive-Extensions / RxJS

The Reactive Extensions for JavaScript
http://reactivex.io
Other
19.49k stars 2.1k forks source link

Type is not inferred correctly using filter operator #1527

Closed rafaelss95 closed 6 years ago

rafaelss95 commented 6 years ago

Current behavior

Consider this piece of code:

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.

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:

this.router.events.subscribe(
  e => {
    if (e instanceof NavigationEnd) {
     // e is inferred as NavigationEnd
     console.log(e);
    }
  }
);