You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Object possibly null in a callback function from a local variable defined inside a function #43827
Typescript thinks found variable inside the asyncFakeSend could be null even though I've already verified it is not with the above if statement and there is no possible way of re-assigning that variable in the function after the fact or at least in the above code sample.
🙂 Expected behavior
No errors.
🙁 Workaround
My coworker suggested I create a new variable from the found inside the function like so:
function getLatestAndSend() {
let found: Item | null = null
for (let i = 0; i < items.length; i++) {
if (!items[i].finished) {
found = items[i]
break
}
}
if (!found) return
let foundToMakeTypeScriptHappy = found
asyncFakeSend(() => {
foundToMakeTypeScriptHappy .finished = true
getLatestAndSend()
})
}
But I've never been a fan of adding code noise that provides no functionality and only exists to make typescript happy. Especially when in above situation, found.finished can never be null (as far as I understood javascript).
I saw that and I scrolled through the issue description but to me it didn't seem relevant to my issue as none of the examples seemed to relate to me (at least to me). Especially since on my end this feels like a bug rather than some logic problem. My bad.
Activity
MartinJohns commentedon Apr 26, 2021
Duplicate of #9998. This issue and many other duplicates can be found with your provided search terms
Object possibly null callback.TheThing commentedon Apr 27, 2021
I saw that and I scrolled through the issue description but to me it didn't seem relevant to my issue as none of the examples seemed to relate to me (at least to me). Especially since on my end this feels like a bug rather than some logic problem. My bad.