I have a service in my Angular app with the following constructor:
constructor(private router: Router, private titleService: Title) {
this.activeMenuItem$ = this.router.events.pipe(
filter(event => event instanceof NavigationEnd),
map(route => {
let active = new MenuItem('test', route.url, null);
return active;
})
);
}
where activeMenuItem$ is an Observable. It works fine on the browser but I'm getting a compilation error:
ERROR in src/app/service/app-menu.service.ts(26,53): error TS2339: Property 'url' does not exist on type 'Event'. Property 'url' does not exist on type 'ActivationEnd'.
But if I try a cast this way:
constructor(private router: Router, private titleService: Title) {
this.activeMenuItem$ = this.router.events.pipe(
filter(event => event instanceof NavigationEnd),
map(route => {
let routeNav = route as NavigationEnd;
let active = new MenuItem('test', routeNav.url, null);
return active;
})
);
}
It works perfectly!.
can anyone explain to me why? Tks