Here is the code snippet:
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")) {
...
}
This triggers a
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 am confident that infinoteUrl
cannot be null
, and I want to inform the compiler about this specific case. Is there a method to do so?
To address the issue, I utilized Optional Chaining (
if (infinoteToken === null && !infinoteUrl?.toString().includes("localhost"))
). However, I am curious about how to instruct the TS compiler that an identified case is a false positive.