Type 'null' is not assignable to type 'string'. Angular/TypeScript

solange

I have an error message:

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

enter image description here

I found a solution here but I didn't understand the answers for my problem.

My code is presented like this:

const expectedRole = route.data['expectedRole'];
const token = localStorage.getItem('token');
    
const { userName, roleId } = decode(token);
console.log(roleId);

enter image description here

Edit 2022-02-02

enter image description here

decode method

export class InvalidTokenError extends Error {}

export interface JwtDecodeOptions {
  header?: boolean;
}

export interface JwtHeader {
  type?: string;
  alg?: string;
}

export interface JwtPayload {
  iss?: string;
  sub?: string;
  aud?: string[] | string;
  exp?: number;
  nbf?: number;
  iat?: number;
  jti?: string;
}

export default function jwtDecode<T = unknown>(
  token: string,
  options?: JwtDecodeOptions
): T;

FYI, I copied the code from the project here

Chris Hamilton

This is just the typescript compiler telling you that token may be null. So you need to check that it isn't before using it in the decode function, since decode does not accept a null parameter.

const expectedRole = route.data['expectedRole'];
const token = localStorage.getItem('token');

if (token) const { userName, roleId } = decode(token);
console.log(roleId);

You can also force the typescript compiler to ignore this using !, which says "this variable will be truthy, trust me", but you need to be absolutely sure it will never be null, or you may get a runtime error.

const expectedRole = route.data['expectedRole'];
const token = localStorage.getItem('token');
    
const { userName, roleId } = decode(token!);
console.log(roleId);

Edit

This solution should work regardless, but you should define the correct return types for decode

const expectedRole = route.data['expectedRole'];
const token = localStorage.getItem('token');

if (token) const info: { userName, roleId } = decode(token);
console.log(info.roleId);

OR

const expectedRole = route.data['expectedRole'];
const token = localStorage.getItem('token');

if (token) const info: any = decode(token);
console.log(info.roleId);

Edit 2

Looks like decode is a generic function, so you can define the return type like this:

const expectedRole = route.data['expectedRole'];
const token = localStorage.getItem('token');

if (token) const { userName, roleId } = decode<{ userName, roleId }>(token);
console.log(roleId);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

TS 2322 Type 'null' is not assignable to type 'string'

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

Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'

type 'string | null' is not assignable to parameter of type 'SetStateAction<string>'.Type 'null' is not assignable to type 'SetStateAction<string>'

Argument of type 'string | null' is not assignable to parameter of type 'String'. Type 'null' is not assignable to type 'String'. [angular]

Type 'string' is not assignable to type '"" | "," | " " | "."'

Type '{}' is not assignable to type 'string'

Type string is not assignable to type

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

Argument type string | null is not assignable to parameter

Why 'null' or 'undefined' "is assignable" to parameter of type 'string'?

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

Type T | null is not assignable to type string | null. How come?

Type 'string | null' is not assignable to type 'string | undefined error with TypeScript

argument of type string | null is not assignable to parameter of type string error

Argument of type 'string | null' is not assignable to parameter of type 'string'

useState and localStorage: argument of type 'string null' is not assignable to parameter of type 'string'

Argument type 'string | null' not assignable to parameter type 'string | number | boolean'

Argument of type 'string | null' is not assignable to parameter of type 'string | undefined'

string[] is not assignable to type string

Type 'null' is not assignable to type 'XXX'

Type 'null' is not assignable to type 'T'

Type 'null' is not assignable to type 'number'

Type 'null' is not assignable to type 'ValidationErrors'

Typescript, JSON.parse error: "Type 'null' is not assignable to type 'string'."

Argument of type '{ name: string; id: null; }' is not assignable to parameter of type 'User'

Argument of type '{ userInfo: string | null; }' is not assignable to parameter of type 'never'

Type 'string' is not assignable