Skip to content
ToolShedby Wasim Shaikh

Angular Interceptor & Guard Converter

Convert class-based Angular HttpInterceptor, CanActivate, CanActivateChild, CanDeactivate, CanMatch, CanLoad and Resolve classes to functional interceptors, guards and resolvers with inject(), plus the provideHttpClient and route wiring.

Examples:
Functional version
Loading the converter…

From Classes to Functional Interceptors, Guards and Resolvers

Modern Angular registers interceptors, route guards and resolvers as plain functions. Class-based guards and resolvers are deprecated, CanLoad has been replaced by CanMatch, and functional interceptors are the recommended way to hook into HttpClient. Paste one or more classes above and the converter turns each into a typed function, swaps constructor injection for inject(), rewrites next.handle(req) to next(req), carries over helper methods and fields, and updates any route definitions in the same file. It also gives you the app.config.ts and app.routes.ts snippets to register them.

Class to Functional Cheat Sheet

Class-basedFunctional
class X implements HttpInterceptorconst x: HttpInterceptorFn = (req, next) => …
next.handle(req)next(req)
class X implements CanActivateconst x: CanActivateFn = (route, state) => …
class X implements CanActivateChildconst x: CanActivateChildFn = (childRoute, state) => …
class X implements CanDeactivate<C>const x: CanDeactivateFn<C> = (component, currentRoute, currentState, nextState) => …
class X implements CanMatchconst x: CanMatchFn = (route, segments) => …
class X implements CanLoad (deprecated)const x: CanMatchFn = (route, segments) => … and canLoad → canMatch
class X implements Resolve<T>const x: ResolveFn<T> = (route, state) => …
constructor(private auth: AuthService)const auth = inject(AuthService);
private helper() { … }const helper = () => { … };
{ provide: HTTP_INTERCEPTORS, useClass: X, multi: true }provideHttpClient(withInterceptors([x]))
canActivate: [AuthGuard]canActivate: [authGuard]

Generating New Functional Guards and Interceptors

The Angular CLI creates the functional versions by default. Use these for new code instead of writing classes.

ng generate interceptor auth
ng generate guard auth --implements CanActivate
ng generate resolver user

Frequently Asked Questions

Are class-based guards and resolvers deprecated?
Yes. Angular deprecated class-based route guards and resolvers in favour of plain functions, and the Route type now lists class tokens under a DeprecatedGuard type. The CanLoad interface is deprecated too, in favour of CanMatch. Class interceptors still work through withInterceptorsFromDi(), but the Angular docs recommend functional interceptors because their order is more predictable in complex setups.
How do injected services work in a function?
Guards, resolvers and interceptors run in an injection context, so you can call inject() at the top of the function. Each constructor parameter or inject() field the class used becomes a const at the top of the function. Calls to inject() must stay at the top level of the function, not inside callbacks such as pipe(map(...)), which is why the converter hoists them.
What happens to the class fields and helper methods?
Helper methods become local functions inside each converted function, so they can still use the injected services. Plain fields move to module scope. Root guards and interceptors were singletons, so a counter or cache they kept was shared between calls, and a module-level variable keeps that behaviour. A field whose initial value reads another member stays inside the function instead.
Why does one class become several functions?
A class can implement CanActivate and CanActivateChild at once, but each function has one job. The converter emits one function per interface, for example authGuard and authChildGuard, and copies the shared helper into each. Register each one under its own route property.
How do I register functional interceptors?
Pass them to provideHttpClient(withInterceptors([authInterceptor, loadingInterceptor])) in app.config.ts, and delete the HTTP_INTERCEPTORS providers. They run in array order for requests and in reverse for responses. If some interceptors are still classes, add withInterceptorsFromDi() as well; the two groups run in the order you list those features.
How do I test a functional guard or interceptor?
For a guard or resolver, configure TestBed with the services it injects and call it with TestBed.runInInjectionContext(() => authGuard(route, state)). For an interceptor, provide provideHttpClient(withInterceptors([authInterceptor])) and provideHttpClientTesting(), then use HttpTestingController to check the outgoing request.
Is my code uploaded anywhere?
No. The conversion runs entirely in your browser.

Sources & Official Resources

The function signatures follow the Angular API reference. Last reviewed , and the converted examples were type-checked and run against Angular 22.2.

Building routes too? The Route Config Builder wires functional guards and resolvers into app.routes.ts, and the inject() Converter migrates constructor injection in components and services. More in Angular tools.