0

JSON.parse(sessionStorage.getItem('owner'))
==> Argument of type 'string | null' is not assignable to parameter of type 'string'. Type 'null' is not assignable to type 'string'.

And the return null;
==> Type 'null' is not assignable to type 'string'.

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; // <== HERE
  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;   // <== HERE: Type 'null' is not assignable to type 'string'.

}

  • 2
    `null` really is not assignable to type `string`. If you want your function to be able to return a string or null, set its return type to `string | null` – Aviad P. Jul 23 '21 at 18:30
  • Does this answer your question? [Argument of type 'string | null' is not assignable to parameter of type 'string'](https://stackoverflow.com/questions/68357873/argument-of-type-string-null-is-not-assignable-to-parameter-of-type-string) – Gaël J Jul 23 '21 at 18:50

1 Answers1

3

You define token() as returning only a string, yet you're trying to return null. You can change the definition to return string | null:

public get token(): string | null {
    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;
}

Of course the code using this getter needs to account for that.

Kelvin Schoofs
  • 8,323
  • 1
  • 12
  • 31