From a3add40570b3e80115be8e761b33e7080a5cc7f3 Mon Sep 17 00:00:00 2001 From: MeAkib Date: Tue, 29 Sep 2026 08:51:39 +0600 Subject: [PATCH] docs: type the external redirect guard `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 function also matches `ProviderToken`, 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. --- docs/src/app/routes.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/docs/src/app/routes.ts b/docs/src/app/routes.ts index b09eb1a8c2a2..d1522d36b875 100644 --- a/docs/src/app/routes.ts +++ b/docs/src/app/routes.ts @@ -7,7 +7,7 @@ */ import {Component} from '@angular/core'; -import {Route, Routes} from '@angular/router'; +import {CanActivateFn, Route, Routes} from '@angular/router'; import {canActivateComponentSidenav} from './pages/component-sidenav/component-sidenav-can-load-guard'; @Component({template: ''}) @@ -95,14 +95,16 @@ function adevRedirect(path: string) { } function externalRedirect(target: string | (() => string)): Partial { + // Typed explicitly, because `canActivate` also accepts deprecated class-based guards. Any function + // matches that type as well, so an inline guard wouldn't have its return value type checked. + const redirect: CanActivateFn = () => { + window.location.href = typeof target === 'string' ? target : target(); + return false; + }; + return { // The router requires a `component`. component: RedirectPlaceholder, - canActivate: [ - () => { - window.location.href = typeof target === 'string' ? target : target(); - return false; - }, - ], + canActivate: [redirect], }; }