Skip to content

docs: type the external redirect guard - #33887

Merged
crisbeto merged 1 commit into
angular:mainfrom
MeAkib:docs/type-external-redirect-guard
Sep 29, 2026
Merged

crisbeto merged 1 commit into
angular:mainfrom
MeAkib:docs/type-external-redirect-guard

Conversation

@MeAkib

@MeAkib MeAkib commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

externalRedirect() passed its guard to canActivate as an inline arrow function. canActivate accepts CanActivateFn as well as the deprecated class-based guards, typed as ProviderToken<any>. Any function also matches ProviderToken<any>, so TypeScript didn't check the inline guard against CanActivateFn. For example, a guard returning a number would still compile.

This declares the guard as a CanActivateFn before passing it to canActivate, so its return value is type checked. The behavior is unchanged. A comment explains why the type is needed, so it isn't inlined again later.

`externalRedirect()` passed its guard to `canActivate` as an inline
arrow function. `canActivate` accepts `CanActivateFn` as well as the
deprecated class-based guards, typed as `ProviderToken<any>`. Any
function also matches `ProviderToken<any>`, so TypeScript didn't check
the inline guard against `CanActivateFn`. For example, a guard returning
a number would still compile.

This declares the guard as a `CanActivateFn` before passing it to
`canActivate`, so its return value is type checked. The behavior is
unchanged. A comment explains why the type is needed, so it isn't
inlined again later.
@pullapprove
pullapprove Bot requested review from crisbeto and ok7sai September 29, 2026 02:53
@angular-robot angular-robot Bot added the area: docs Related to the documentation label Sep 29, 2026
@crisbeto crisbeto added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Sep 29, 2026
@crisbeto
crisbeto merged commit 1543543 into angular:main Sep 29, 2026
35 checks passed
@crisbeto

Copy link
Copy Markdown
Member

This PR was merged into the repository. The changes were merged into the following branches:

crisbeto pushed a commit that referenced this pull request Sep 29, 2026
`externalRedirect()` passed its guard to `canActivate` as an inline
arrow function. `canActivate` accepts `CanActivateFn` as well as the
deprecated class-based guards, typed as `ProviderToken<any>`. Any
function also matches `ProviderToken<any>`, so TypeScript didn't check
the inline guard against `CanActivateFn`. For example, a guard returning
a number would still compile.

This declares the guard as a `CanActivateFn` before passing it to
`canActivate`, so its return value is type checked. The behavior is
unchanged. A comment explains why the type is needed, so it isn't
inlined again later.

(cherry picked from commit 1543543)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: docs Related to the documentation target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants