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
functionfoo(){leticon=document.getElementById("icon");if(!icon)return;// From here, the local variable icon is defined and not nullicon.addEventListener("click",()=>{letbar=icon.id;// error TS2531: icon is possibly null},true);}
Expected behavior:
In the code above, the local variable icon cannot be null after the if statement, unless I miss something. Although there is a closure in the addEventListener call, and this is an asynchronous function, I fail to see how and when such a variable icon could be null since there is no other reference to this variable until the end of the function foo. I am wondering how difficult it would be to check the non-nullability of closure variables in such a simple case. This would prevent some useless errors/warnings and would avoid the usage of the ! operator.
Actual behavior:
The compiler emits an error TS2531: Object [icon] is possibly null
The text was updated successfully, but these errors were encountered:
Code
Expected behavior:
In the code above, the local variable
icon
cannot be null after theif
statement, unless I miss something. Although there is a closure in theaddEventListener
call, and this is an asynchronous function, I fail to see how and when such a variableicon
could be null since there is no other reference to this variable until the end of the functionfoo
. I am wondering how difficult it would be to check the non-nullability of closure variables in such a simple case. This would prevent some useless errors/warnings and would avoid the usage of the!
operator.Actual behavior:
The compiler emits an error TS2531: Object [icon] is possibly null
The text was updated successfully, but these errors were encountered: