Home > Net >  Type 'string | boolean | null' is not assignable to type 'boolean'. Type 'n
Type 'string | boolean | null' is not assignable to type 'boolean'. Type 'n

Time:01-03

What's wrong with this code?

isAuthenticated(): boolean
  {
    var token = localStorage.getItem(ACCESS_TOKEN_KEY);
    return token && !this.jwtHelper.isTokenExpired(token);
  }

Error:

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

CodePudding user response:

If you want to return boolean, you need to cast these variables to a boolean value. For example:

isAuthenticated(): boolean
  {
    var token = localStorage.getItem(ACCESS_TOKEN_KEY);
    return !!token && !!this.jwtHelper.isTokenExpired(token);
  }
  • Related