0
votes

I am trying to implement simple canActivate in my Auth service

import {CanActivate,ActivatedRouteSnapshot} from '@angular/router';
export class Auth implements CanActivate{
    canActivate(
        next: ActivatedRouteSnapshot,
        state: ActivatedRouteSnapshot
      ): Observable<boolean> {

            return Observable.of(true);
      }
}

got this error

ERROR in src/app/modules/user/auth.ts(13,14): error TS2420: Class 'Auth' incorrectly implements interface 'CanActivate'. Types of property 'canActivate' are incompatible. Type '(next: ActivatedRouteSnapshot, state: ActivatedRouteSnapshot) => Observable' is not assignable to type '(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => boolean | Promise | Obser...'. Types of parameters 'state' and 'state' are incompatible. Type 'RouterStateSnapshot' is not assignable to type 'ActivatedRouteSnapshot'. Types of property 'url' are incompatible. Type 'string' is not assignable to type 'UrlSegment[]'.

2

2 Answers

4
votes

I think the problem is the declaration of state: should be RouterStateSnapshot

interface CanActivate { 
   canActivate(route: ActivatedRouteSnapshot, 
               state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean
}
2
votes

Change the type of state to be RouterStateSnapshot. That is the only thing the compiler complains about.