Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string local storage' code example

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

this.currentUser = JSON.parse(localStorage.getItem('currentUser') || '{}');

Example 2: JSON.parse(localStorage.getItem('users') Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'.

this.currentUser = JSON.parse(localStorage.getItem('currentUser')!);

Tags:

Misc Example