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
This is probably technically the correct behavior. However, for the specific case of window, this is problematic. There are globals like ontouchstart which are declared as being var ontouchstart: whatever | undefined in the global scope; these get intersected with the global Window type, and because we believe "variables can't be optional", we start narrowing to never in the else branch here.
It's unclear what we should do differently here, but this caused a break in a few different places in real world code where people are doing feature detection. We're incorrectly telling people that e.g. ontouchstart is always present, even though you would get an error to unconditionally invoke it (because it is indeed possibly-undefined).
Since there's still no "optional var" and still no "missing vs undefined", we should maybe treat properties that include undefined as "possibly not in" for the purposes of narrowing.
See discussion at the fix; briefly, the minimal fix for this bug is the right one because the real fix for the whole feature is to introduce the missing type.
TypeScript Version: 3.8.3 vs nightly
Search Terms: intersection in operator
Code
Expected behavior: Well...
This is probably technically the correct behavior. However, for the specific case of
window
, this is problematic. There are globals likeontouchstart
which are declared as beingvar ontouchstart: whatever | undefined
in the global scope; these get intersected with the globalWindow
type, and because we believe "variables can't be optional", we start narrowing tonever
in the else branch here.It's unclear what we should do differently here, but this caused a break in a few different places in real world code where people are doing feature detection. We're incorrectly telling people that e.g.
ontouchstart
is always present, even though you would get an error to unconditionally invoke it (because it is indeed possibly-undefined
).Since there's still no "optional var" and still no "missing vs undefined", we should maybe treat properties that include undefined as "possibly not
in
" for the purposes of narrowing.Actual behavior: Described
Playground Link: Link
Related Issues: Possible root cause: #37106
The text was updated successfully, but these errors were encountered: