Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object possibly null in a callback function from a local variable defined inside a function #43827

Closed
TheThing opened this issue Apr 26, 2021 · 2 comments · Fixed by #56908
Closed
Labels
Duplicate An existing issue was already created

Comments

@TheThing
Copy link

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

interface Item { name: string, finished: boolean }

let items: Array<Item> = []

function asyncFakeSend(cb: Function) {
  setTimeout(cb, 100)
}

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

  asyncFakeSend(() => {
    found.finished = true
    getLatestAndSend()
  })
}

🙁 Actual behavior

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).

@MartinJohns
Copy link
Contributor

MartinJohns commented Apr 26, 2021

Duplicate of #9998. This issue and many other duplicates can be found with your provided search terms Object possibly null callback.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 26, 2021
@TheThing
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants