Md Ejaz
Md Ejaz

Reputation: 21

I got error Type 'null' is not assignable to type 'string'

Error: src/app/MyComponents/todos/todos.component.ts:13:5 - error TS2322: Type 'string | null' is not assignable to type 'string'. is not assignable to type 'string'. Type 'null' is not assignable to type 'string'.

13 this.localItem = localStorage.getItem("todos");

Upvotes: 0

Views: 702

Answers (2)

Girija
Girija

Reputation: 118

Make sure that while setting the local storage item we should have called the token in the response like,

localStorage.setItem("jwt", response.token);

You have passed the token as the second parameter.

Go to developer tools in Google chrom and check for the local storage whether the name in the local storage and the name you have passed here are the same.

Upvotes: 0

zainhassan
zainhassan

Reputation: 1780

localStorage.getItem can return null. You need to add null check.

const todos = localStorage.getItem("todos");
if (todos) {
    this.localItem = todos;
}

Upvotes: 1

Related Questions