When trying to parse the stored session data using JSON.parse(sessionStorage.getItem('owner')), we may encounter an error stating: Argument of type 'string | null' is not assignable to parameter of type 'string'. This is because the function expects a string but receives a value that is potentially null, which is not compatible with a string type.
If the code returns null when accessing the owner, it triggers another issue where null cannot be assigned to a variable of type 'string', leading to a type mismatch error.
public get owner(): Owner {
if (this._owner != null) {
return this._owner;
} else if (this._owner == null && sessionStorage.getItem('owner') != null) {
this._owner = JSON.parse(sessionStorage.getItem('owner')) as Owner; // Check this line for potential errors.
return this._owner;
}
return new Owner();
}
public get token(): string {
if (this._token != null) {
return this._token;
} else if (this._token == null && sessionStorage.getItem('token') != null) {
this._token = sessionStorage.getItem('token') as string;
return this._token;
}
return null; // Be cautious here as returning null might lead to a type mismatch error.
}