In the code below (an example from the book "Angular 2 Development with TypeScript"):
import {CanDeactivate, Router} from "@angular/router";
import {Injectable} from "@angular/core";
@Injectable()
export class UnsavedChangesGuard implements CanDeactivate{
constructor(private _router:Router){}
canDeactivate(){
return window.confirm("You have unsaved changes. Still want to leave?");
}
}
I see a warning when I hover over CanDeactivate in WebStorm:
Referring to the answers to this question - Generic type 'Observable<T>' requires 1 type argument - the following change removes the warning:
export class UnsavedChangesGuard implements CanDeactivate<any>{
However, I would like to know how I can find out what is the actual argument that CanDeactivate requires.
Edit: Looking at @angular/router/src/interfaces.d.ts we can see the following:
/**
* @whatItDoes Indicates that a class can implement to be a guard deciding if a route can be
* deactivated.
*
* @howToUse
*
* ```
* class UserToken {}
* class Permissions {
* canDeactivate(user: UserToken, id: string): boolean {
* return true;
* }
* }
*
* @Injectable()
* class CanDeactivateTeam implements CanDeactivate<TeamComponent> {
* constructor(private permissions: Permissions, private currentUser: UserToken) {}
*
* canDeactivate(
* component: TeamComponent,
* route: ActivatedRouteSnapshot,
* state: RouterStateSnapshot
* ): Observable<boolean>|Promise<boolean>|boolean {
* return this.permissions.canDeactivate(this.currentUser, route.params.id);
* }
* }
*
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* canDeactivate: [CanDeactivateTeam]
* }
* ])
* ],
* providers: [CanDeactivateTeam, UserToken, Permissions]
* })
* class AppModule {}
* ```
*
* You can also provide a function with the same signature instead of the class:
*
* ```
* @NgModule({
* imports: [
* RouterModule.forRoot([
* {
* path: 'team/:id',
* component: TeamCmp,
* canActivate: ['canDeactivateTeam']
* }
* ])
* ],
* providers: [
* {
* provide: 'canDeactivateTeam',
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true
* }
* ]
* })
* class AppModule {}
* ```
*
* @stable
*/
export interface CanDeactivate<T> {
canDeactivate(component: T, route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean;
}
But it's not clear what "TeamComponent" is.