for-of loop with intersection of array types produces a union of element types #39693
Labels
Fix Available
A PR has been opened for this issue
In Discussion
Not yet reached consensus
Suggestion
An idea for TypeScript
TypeScript Version: 3.9.2, 4.0.0-beta
Search Terms: intersection, for-of, union, iterable, iterator, iterate, array
Expected behavior:
When you use a
for..of
loop to iterate over the elements of an intersection of arrays (or maybe other iterables), what type should the elements be? I would expect either:Actual behavior:
Iterating over an intersection of arrays with a
for..of
loop produces a union of their element types for some reason.Related Issues:
#11961: intersection of array types results in overloaded methods (this would maybe imply overloaded iterators, but that's not happening here)
Aside:
I'm not sure why you'd want an intersection of array types in the first place; but the behavior showed up in a Stack Overflow question and I'm at a loss understanding why we get a union here.
Code
Output
Compiler Options
Playground Link: Provided
The text was updated successfully, but these errors were encountered: