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
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
constfooMap:Map<string,Array<number>>=newMap()functionsomeFunction(arg:Array<number>){//...}constvalues=[1,2,3,4,5];letfoo=fooMap.get("a");if(foo==null){foo=[];}// The issue is this line, where TS says that `foo` may be undefinedvalues.forEach((v)=>foo.push(v));// In this line, TS recognizes that `foo` is not undefinedsomeFunction(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?
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).
Activity
MartinJohns commentedon Jan 4, 2023
Duplicate of #9998.
mqualizz commentedon 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 thevalues.forEach()function call). Even if it were to be argued that we can't determine whethervalues.forEach()modifiesfoo, and thusfoocould beundefinedat thesomeFunction()line, that's not what's happening --foois being marked as possiblyundefinedin the callback (and not at thesomeFunction(foo)line).What am I missing?
MartinJohns commentedon Jan 4, 2023
Narrowing is reset in closures. It is a duplicate.
mqualizz commentedon Jan 4, 2023
Ok, thank you.