Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): remove RouterEvent from Event union type #46061

Closed
wants to merge 1 commit into from

Conversation

cexbrayat
Copy link
Member

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

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).

What is the new behavior?

This commit fixes the issue by removing RouterEvent from the union type.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

@pullapprove pullapprove bot requested a review from atscott May 19, 2022 13:55
@cexbrayat cexbrayat requested review from clydin and removed request for atscott May 19, 2022 13:55
@pullapprove pullapprove bot requested a review from atscott May 19, 2022 13:55
@atscott atscott added the target: rc This PR is targeted for the next release-candidate label May 19, 2022
@atscott
Copy link
Contributor

atscott commented May 19, 2022

@cexbrayat This was actually done intentionally due to concerns around introducing a breaking change and time pressure with the v14 feature freeze. I've triggered internal presubmits to see how breaking this change really would be.

@atscott atscott added the action: global presubmit The PR is in need of a google3 global presubmit label May 19, 2022
@atscott
Copy link
Contributor

atscott commented May 20, 2022

As expected, this is a breaking change. In particular, it would break this particular pattern to filter to the parent type (for example, if you want any event with an id or url):

            filter(
                (e: NavigationEvent): e is RouterEvent =>
                    e instanceof RouterEvent),

While we could consider not supporting this use-case in the same way, it's certainly not something we can just squeeze in to the v14 release since the RC is out and we can't introduce new breaking changes.

I'm going to close this since it's not feasible to land now. Could you open a feature request so we can gauge community interest and determine how much we should prioritize it for v15?

@atscott atscott closed this May 20, 2022
@cexbrayat
Copy link
Member Author

@atscott I don't think it's particularly useful, it's just that the addition of type (and its intended goal) is not really usable right now. Let's keep it close then. Maybe it should just be removed from the changelog https://github.com/angular/angular/blob/main/CHANGELOG.md#1400-next16-2022-05-04 ?

@atscott
Copy link
Contributor

atscott commented May 20, 2022

@cexbrayat The intent was a resolution for #43762. Removing it from the changelog doesn't make sense because the change is merged and committed and we won't be reverting it. It's not as useful as it could be, but it's not entirely useless.

@clydin
Copy link
Member

clydin commented May 20, 2022

You should be able to use this form instead with the filter:
this.router.events.pipe(filter((event: Event): event is NavigationEnd => 'type' in event && event.type === EventType.NavigationEnd)).subscribe(/*...*/);

The need to hint that the filter function is type narrowing is unfortunately verbose in either case. It may be useful to introduce a series of helper functions (e.g., isNavigationEnd() or similar) in the future to more easily support the filter use cases.
This should reduce the code to something approaching:
this.router.events.pipe(filter(isNavigationEnd).subscribe(/*...*/);

@cexbrayat
Copy link
Member Author

Thanks @clydin , that's what I'm currently using. I just thought leaving RouterEvent in the union type was unintended as the changelog led me to think 'type' in event would not be necessary 👍

Thanks @atscott for your time and explanation

@atscott
Copy link
Contributor

atscott commented May 20, 2022

@cexbrayat To be clear, I do think this is worth a feature request. You're absolutely right that the experience is not ideal at the moment.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 20, 2022
@angular angular unlocked this conversation Oct 5, 2022
@clydin
Copy link
Member

clydin commented Oct 5, 2022

@cexbrayat Would you be interesting in reopening and rebasing this change? We should hopefully be able to land this in v15.

@cexbrayat
Copy link
Member Author

@clydin Sure, done!

@pullapprove pullapprove bot removed the request for review from atscott October 6, 2022 16:39
@ngbot ngbot bot added this to the Backlog milestone Oct 6, 2022
@atscott
Copy link
Contributor

atscott commented Nov 14, 2022

@cexbrayat The team was really busy with a bunch of talks and conferences leading up to v15 so we didn't have time to do the internal cleanup necessary to land this before the feature freeze several weeks back. That said, we'll definitely get this in to v16. I'm doing the cleanup now and will add a patch to include this change so we don't have any regressions and will be able to merge as soon as the main branch is targeting v16.

That said, can you add the BREAKING CHANGE footer to your commit? Removing RouterEvent from the Event union type did result in a couple dozen build failures, mostly in tests with stubs. Sometimes this did extend to types used in production code, for example: filter((e: Event): e is RouterEvent => e instanceof RouterEvent) needed to change to filter((e: Event|RouterEvent): e is RouterEvent => e instanceof RouterEvent)

@atscott atscott added action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed target: rc This PR is targeted for the next release-candidate labels Nov 14, 2022
@angular-robot angular-robot bot added the detected: breaking change PR contains a commit with a breaking change label Nov 14, 2022
@cexbrayat
Copy link
Member Author

@atscott Hi Andrew. No problem, I amended the commit, quoting your example as a use-case. Let me know if that's OK.

@atscott
Copy link
Contributor

atscott commented Nov 30, 2022

quick update: This change has been patched into Google's codebase. That means there will be no internal cleanup needed when we're ready to merge this to the v16 branch.

@cexbrayat cexbrayat force-pushed the fix/router-event branch 3 times, most recently from 1925918 to 77093a4 Compare January 12, 2023 23:19
Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one tiny non-code request: Can we update the commit message to describe the final result rather than the existing problem? i.e. "fix(router): Remove RouterEvent from Event type union to ..." or something to that effect

@atscott atscott added target: major This PR is targeted for the next major release and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews action: global presubmit The PR is in need of a google3 global presubmit labels Jan 17, 2023
Copy link
Contributor

@atscott atscott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: public-api

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)`.
@cexbrayat
Copy link
Member Author

@atscott Sure, done 👍

@cexbrayat cexbrayat changed the title fix(router): duplicate RouterEvent in union type fix(router): remove RouterEvent from Event union type Jan 18, 2023
Copy link
Contributor

@jessicajaniuk jessicajaniuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed-for: public-api

@atscott atscott added action: merge The PR is ready for merge by the caretaker merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note labels Feb 17, 2023
@atscott
Copy link
Contributor

atscott commented Feb 17, 2023

merge assistance: This change is already patched in to g3. When syncing this commit, you'll need to also remove the router_events.patch (http://cl/510469344).

@alxhub
Copy link
Member

alxhub commented Feb 22, 2023

This PR was merged into the repository by commit 1e32709.

@alxhub alxhub closed this in 1e32709 Feb 22, 2023
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Mar 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: router breaking changes detected: breaking change PR contains a commit with a breaking change merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants