angular: Observable is returning a wrong-sounding 'Event_2' type
Which @angular/* package(s) are the source of the bug?
router
Is this a regression?
No
Description
router.events
Observable is returning a ‘Event_2’ type in the API
This seems like an issue with the API Generator. It’s causing autocomplete to hint and add the wrong ‘Event_2’ type to code.
Please provide a link to a minimal reproduction of the bug
https://github.com/angular/angular/blob/master/goldens/public-api/router/router.md
Please provide the exception or error you saw
`readonly events: Observable<Event_2>;`
Please provide the environment you discovered this bug in
No response
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 21 (5 by maintainers)
Thanks @JoostK, you’re right about it being an issue with the autocomplete to try and add add unexported
Event_2
.It’s definitely a real issue, because the hinting itself* is going to lend to folks adding
Event_2
to their own docs/code comments.I agree that API Extractor should be smarter here to use the exported type name if available instead of an inaccessible internal type. I’ll open an issue in the API Extractor project.
As a stop gap, is it possible to make the Observable return the
Event
type? It would be straightforward for every tool’s logic to get it right…*Insofar as any type hinting logic is concerned, it would not be the prerogative of the tool to resolve internal types to available exported ones, the internal types can be exported multiple times with different names or not at all.
@IvanAmdaris You can drop the
map()
and use a predicate instead !Hi,
I have the same issue. “Event_2” type for “RouterEvent”.
Original code.
After removing “RouterEvent”.
I’m sharing the workaround that worked for me: the error:
that generates:
Basically wrapping filter function of rxjs inside the pipe function:
Version 16
make sure that
Event
should be imported from ‘@angular/router’;I was having a similar issue where
produced the following error during build:
In the Angular Update Guide, one of the bullet points for updating an app from Angular 15 to 16 is:
Following this guidance, I changed the line from
to
and this resolved the issue for me.
+1
Just my two cents about this issue, the
Event
type is part of the router package and furthermore it could be possible you need also a type guard to be able to use that event later.Nothing in here was working for me, here is what I had to use - kind of odd router.events if of type
Event_2
I even updated angular/core to ^16.2.1 today to confirm this wasn’t some regression from v16.x.x that was already fixedThanks for replying so fast, nice to see people do that here, I tried that and it seems to be working.
What also seems to be working is just use
Event
instead ofEvent_2
the only thing that I haven’t noticed was that when writingEvent
the auto import wouldn’t trigger for me so if you find yourself in my situation make sure toimport { Event } from '@angular/router';
This one worked for me but I haven’t tested yet. Sorry for not sharing.
Hi ,
I am getting the same issues with the Event_2 type for RouterEvent. How could we possibly fix it?