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
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).
The text was updated successfully, but these errors were encountered:
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.
Bug Report
🔎 Search Terms
Object possibly null callback
Object possibly null function local
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about functions and variables.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Typescript thinks
found
variable inside theasyncFakeSend
could be null even though I've already verified it is not with the aboveif
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: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).The text was updated successfully, but these errors were encountered: