Skip to content

Commit 1e32709

Browse files
cexbrayatalxhub
authored andcommitted
fix(router): remove RouterEvent from Event union type (#46061)
41e2a68 added a `type` property on all router events, and added all type of events to the `Event` union type, but forgot to remove `RouterEvent`. This removes the benefit of the `type` field, because it is not possible to write: ``` this.router.events.pipe(filter((event: Event): event is NavigationEnd => event.type === EventType.NavigationEnd)).subscribe(/*...*/); ``` as `RouterEvent` does not have a `type` field (hence TS complains). This commit fixes the issue by removing `RouterEvent` from the union type. BREAKING CHANGE: The `RouterEvent` type is no longer present in the `Event` union type representing all router event types. If you have code using something like `filter((e: Event): e is RouterEvent => e instanceof RouterEvent)`, you'll need to update it to `filter((e: Event|RouterEvent): e is RouterEvent => e instanceof RouterEvent)`. PR Close #46061
1 parent 02cd490 commit 1e32709

File tree

3 files changed

+7
-11
lines changed

3 files changed

+7
-11
lines changed

goldens/public-api/router/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export type DisabledInitialNavigationFeature = RouterFeature<RouterFeatureKind.D
251251
export type EnabledBlockingInitialNavigationFeature = RouterFeature<RouterFeatureKind.EnabledBlockingInitialNavigationFeature>;
252252

253253
// @public
254-
type Event_2 = RouterEvent | NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized | GuardsCheckStart | GuardsCheckEnd | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd | ActivationStart | ActivationEnd | Scroll | ResolveStart | ResolveEnd | NavigationSkipped;
254+
type Event_2 = NavigationStart | NavigationEnd | NavigationCancel | NavigationError | RoutesRecognized | GuardsCheckStart | GuardsCheckEnd | RouteConfigLoadStart | RouteConfigLoadEnd | ChildActivationStart | ChildActivationEnd | ActivationStart | ActivationEnd | Scroll | ResolveStart | ResolveEnd | NavigationSkipped;
255255
export { Event_2 as Event }
256256

257257
// @public

packages/router/src/events.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -629,16 +629,12 @@ export class Scroll {
629629
*
630630
* @publicApi
631631
*/
632-
export type Event = RouterEvent|NavigationStart|NavigationEnd|NavigationCancel|NavigationError|
633-
RoutesRecognized|GuardsCheckStart|GuardsCheckEnd|RouteConfigLoadStart|RouteConfigLoadEnd|
634-
ChildActivationStart|ChildActivationEnd|ActivationStart|ActivationEnd|Scroll|ResolveStart|
635-
ResolveEnd|NavigationSkipped;
636-
632+
export type Event = NavigationStart|NavigationEnd|NavigationCancel|NavigationError|RoutesRecognized|
633+
GuardsCheckStart|GuardsCheckEnd|RouteConfigLoadStart|RouteConfigLoadEnd|ChildActivationStart|
634+
ChildActivationEnd|ActivationStart|ActivationEnd|Scroll|ResolveStart|ResolveEnd|
635+
NavigationSkipped;
637636

638637
export function stringifyEvent(routerEvent: Event): string {
639-
if (!('type' in routerEvent)) {
640-
return `Unknown Router Event: ${routerEvent.constructor.name}`;
641-
}
642638
switch (routerEvent.type) {
643639
case EventType.ActivationEnd:
644640
return `ActivationEnd(path: '${routerEvent.snapshot.routeConfig?.path || ''}')`;

packages/router/test/integration.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6376,7 +6376,7 @@ describe('Integration', () => {
63766376
[{path: 'user/:name', component: UserCmp}, {path: 'simple', component: SimpleCmp}]
63776377
}]);
63786378

6379-
const events: RouterEvent[] = [];
6379+
const events: Event[] = [];
63806380
router.events.subscribe(e => e instanceof RouterEvent && events.push(e));
63816381

63826382
// supported URL
@@ -6440,7 +6440,7 @@ describe('Integration', () => {
64406440
[{path: 'user/:name', component: UserCmp}, {path: 'simple', component: SimpleCmp}]
64416441
}]);
64426442

6443-
const events: RouterEvent[] = [];
6443+
const events: Event[] = [];
64446444
router.events.subscribe(e => e instanceof RouterEvent && events.push(e));
64456445

64466446
location.simulateHashChange('/include/user/kate(aux:excluded)');

0 commit comments

Comments
 (0)