The following code
const infinoteUrl =
$q.localStorage.getItem("infinote-dev-api") === null
? `${window.location.protocol}//${window.location.host}`
: $q.localStorage.getItem("infinote-dev-api")
console.log(`infinote URL: ${infinoteUrl}`)
let infinoteToken = $q.localStorage.getItem("infinote-token")
if (infinoteToken === null && !infinoteUrl.toString().includes("localhost")) {
...
}
compiles with the TS2531: Object is possibly 'null' error:
TS2531: Object is possibly 'null'.
106 |
107 | let infinoteToken = $q.localStorage.getItem("infinote-token")
> 108 | if (infinoteToken === null && !infinoteUrl.toString().includes("localhost")) {
| ^^^^^^^^^^^
109 | infinoteToken = "empty"
110 | $q.notify({
111 | message: 'no infinote-token in local storage',
I do not belive that infinoteUrl can ever be null so I would like to tell the compiler that this case is fine. Is there a way to do so?
As for the issue itself, I solved it thanks to Optional Chaining (if (infinoteToken === null && !infinoteUrl?.toString().includes("localhost"))), but I am interested how to more generally tell the TS compiler that a case it detected is a false alert.