typescriptjwtangular11

Type 'string' | null is not assignable to type 'string'


I am using angular-jwt to to check whether token is expire or not and while using i am getting this error

Type 'string' | null is not assignable to type 'string'

helper = new JwtHelperService();

var token: string = localStorage.getItem('token');
this.helper.isTokenExpired(token); // this line contains the error

Error

enter image description here

enter image description here


Solution

  • First you should check your token

        var token: string = localStorage.getItem('token');
        if (token) {
           this.helper.isTokenExpired(token);
        }