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
Bug diagnosing error "Object is possibly 'undefined'" #31344
functionSum(A?: number){constAisUndefined: boolean=typeofA==="undefined";if(AisUndefined){return'is undefined';}constsum: number=A+10;//Error here: "Object is possibly 'undefined'"returnsum;}
Expected behavior: As I assign the condition typeof A === "undefined" in the constant AisUndefined and make a if statement to return a string in case of A is definitely undefined, in the constant sum, there should be no problem making a sum of A with a number assuming that A is definitely not undefined as the code reach there, after the if statement.
Actual behavior: When I try to make a sum with A and a number, and probably any other operation that takes on that A is not undefined, typescript identifies this as an error, saying that Object is possibly 'undefined', basically ignoring the return inside the if statement.
Note: If I replace the constant AisUndefined by their own condition, like:
Activity
jack-williams commentedon May 10, 2019
Duplicate of #12184
fatcerberus commentedon May 12, 2019
Yeah, this is normal. Only direct type guards work; the compiler doesn't track the effect of assigning a type check result to another variable.