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 unexpectedly undefined #52104

Closed
mqualizz opened this issue Jan 4, 2023 · 4 comments · Fixed by #56908
Closed

Object unexpectedly undefined #52104

mqualizz opened this issue Jan 4, 2023 · 4 comments · Fixed by #56908
Labels
Duplicate An existing issue was already created

Comments

@mqualizz
Copy link

mqualizz commented Jan 4, 2023

Bug Report

🔎 Search Terms

undefined object, callback, closure, hoisting, arrow function

🕗 Version & Regression Information

  • This is the behavior in every version I tried (every version available in the playground, from 3.3.3 to Nightly 5.0.0), and I reviewed the FAQ for entries about undefined, closure, hoisting

⏯ Playground Link

Playground link with relevant code

💻 Code

const fooMap:Map<string,Array<number>> = new Map()

function someFunction(arg:Array<number>) {
    //...
}

const values = [1, 2, 3, 4, 5];

let foo = fooMap.get("a");

if (foo == null) {
    foo = [];
}

// The issue is this line, where TS says that `foo` may be undefined
values.forEach((v) => foo.push(v));

// In this line, TS recognizes that `foo` is not undefined
someFunction(foo);

🙁 Actual behavior

A variable (in this case, foo) is being marked as possibly undefined when used inside of a closure, although it will never be null at that point.

🙂 Expected behavior

I expect foo to be not-null.

I apologize if the observed behaviour is expected -- but, if it is, can someone explain why?

@MartinJohns
Copy link
Contributor

Duplicate of #9998.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 4, 2023
@mqualizz
Copy link
Author

mqualizz commented Jan 4, 2023

Is this a duplicate? I've read the bug, and am still reading through all of the comments, but I don't believe this is a situation where control flow analysis is having to contend with a function call.

The problem isn't someFunction(foo) (which happens after the values.forEach() function call). Even if it were to be argued that we can't determine whether values.forEach() modifies foo, and thus foo could be undefined at the someFunction() line, that's not what's happening -- foo is being marked as possibly undefined in the callback (and not at the someFunction(foo) line).

What am I missing?

@MartinJohns
Copy link
Contributor

Narrowing is reset in closures. It is a duplicate.

@mqualizz
Copy link
Author

mqualizz commented Jan 4, 2023

Narrowing is reset in closures. It is a duplicate.

Ok, thank you.

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