TS2416: Property 'canActivate' in type 'MyGuard' is not assignable to the same property in base type 'CanActivate'

15,405

Solution 1

The error you are facing is because you copied the code that is for a different version of Angular than what you are using.

The error went away when you removed the return value because you made the function compatible with the version you have on your machine.

You can check the function signature at your current version by navigating to CanActivate on your machine. If you are using Visual Studio Code, you can press Ctrl and click on it to navigate to its file.

Solution 2

@user911 - I recently started learning angular and fortunately came up with the same issue.

The reason for the error would probably be that your IDE accidentally imported Promise from 'q' import {Promise} from 'q'; remove this and you can even declare the return type of the canActivate method which is Observable< boolean> | Promise< boolean> | boolean.

The import is the only reason for which your app works fine when you remove the return type of the canActivate method.

Try this for better understanding:

  1. Make sure you define the return type of the canActivate method and while defining the type let IDE automatically import the Promise from q or import it manually.

  2. As we expect there will be error and now remove Promise< boolean> from the return type and the error should go away unless you are returning a promise with your canActivate method.

Share:
15,405
user911
Author by

user911

Updated on July 26, 2022

Comments

  • user911
    user911 almost 2 years

    I have written an angular 4.3.0 typescript library. While building my library I saw below error in *.d.ts file.

    ERROR in [at-loader] ..\myLibrary\lib-commonjs\my-guard.service.d.ts:13:5 TS2416: Property 'canActivate' in type 'MyGuard' is not assignable to the same property in base type 'CanActivate'. Type '(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) => boolean | Promise | Observ...' is not assignable to type '(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => boolean | Observable | Pr...'. Type 'boolean | Promise | Observable' is not assignable to type 'boolean | Observable | Promise'. Type 'Observable' is not assignable to type 'boolean | Observable | Promise'. Type 'Observable' is not assignable to type 'Promise'. Property '[Symbol.toStringTag]' is missing in type 'Observable'.

    This is how my guard looks like

      @Injectable()
        export class MyGuard implements CanActivate {
             canActivate( next: ActivatedRouteSnapshot ,state: RouterStateSnapshot):  Observable<boolean> | Promise<boolean> | boolean  {
    return true;
            }
        }
    

    The error goes away after I removed the return type (Observable | Promise | boolean ) from canActivate. I want to understand why I need to remove it to make it work.

     canActivate( next: ActivatedRouteSnapshot ,state: RouterStateSnapshot)  {
        }
    

    Error

  • daniel
    daniel over 5 years
    canActivate():any { ... did it for me
  • Aluan Haddad
    Aluan Haddad almost 5 years
    This answer is wrong. Boolean and boolean are distinct types. true is a boolean.
  • Aluan Haddad
    Aluan Haddad almost 5 years
    @daniel that's wrong. It will allow you to right completely incorrect code and bypass any checking.
  • Nico Haase
    Nico Haase almost 5 years
    Please add some explanation to your code - what does it do, and why does it solve the question?
  • Aluan Haddad
    Aluan Haddad almost 5 years
    You might want to add that rxjs and @angular/whatever to peerDependencies in order to ensure that the cause your answer correctly infers is eliminated.
  • Matthew
    Matthew over 4 years
    Or right click "CanActivate" in the import statement and click "Go to Definition". In mine the return value is defined as : Observable<boolean> | Promise<boolean> | boolean
  • AndrewBenjamin
    AndrewBenjamin almost 4 years
    I've tried adding the same versions to peerDependencies, ensuring the same version in all package.json and going as far as to side-by-side compare of the entire @angular folder, deleting the entire node_modules folder, upgrading typescript and restarting the computer. Still this frustrating message persists. The only thing that worked was copying an identical project where the error didn't exist and starting from scratch. Whatever's triggering this is flippin' buried.