Repository: TypeScript
Issue: for-of loop with intersection of array types produces a union of element types
Labels: Suggestion,In Discussion,Fix Available

**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:
* you get the same type as when you index into the array: an intersection of the element types; or
* you get the same type as when you use the iterator method manually: the first element type because the iterator methods are overloads.

**Actual behavior:**
Iterating over an intersection of arrays with a `for..of` loop produces a *union* of their element types for some reason.

<!-- Did you find other bugs that looked similar? -->
**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](https://stackoverflow.com/questions/63010203/typescript-intersection-fails-on-list-for-loop) and I'm at a loss understanding why we get a union here.


**Code**
```ts
declare const arr: Array<{ a: string }> & Array<{ b: number }>;

for (const elemItr of arr) {
  // { a: string } | {b : number } 😕
  elemItr.a.toUpperCase(); // error!
  elemItr.b.toFixed(); // error!
}

// I expected either this (intersection of element types)
const elemIdx = arr[0]; // { a: string; } & { b: number; }
elemIdx.a.toUpperCase(); // okay
elemIdx.b.toFixed(); // okay

// or this (overloaded iterators giving the first element type only)
const iter = arr[Symbol.iterator];
/* const iter: {
    (): IterableIterator<{ a: string }>;
    (): IterableIterator<{ a: string }>;
} & {
    (): IterableIterator<{ b: number }>;
    (): IterableIterator<{ b: number }>;
} */
const result = arr[Symbol.iterator]().next();
if (!result.done) {
  result.value.a.toUpperCase(); // okay
  result.value.b.toFixed(); // error, expected this 
}
```



<details><summary><b>Output</b></summary>

```ts
"use strict";
for (const elemItr of arr) {
    // { a: string } | {b : number } 😕
    elemItr.a.toUpperCase(); // error!
    elemItr.b.toFixed(); // error!
}
// I expected either this (intersection of element types)
const elemIdx = arr[0]; // { a: string; } & { b: number; }
elemIdx.a.toUpperCase(); // okay
elemIdx.b.toFixed(); // okay
// or this (overloaded iterators giving the first element type only)
const iter = arr[Symbol.iterator];
/* const iter: {
    (): IterableIterator<{ a: string }>;
    (): IterableIterator<{ a: string }>;
} & {
    (): IterableIterator<{ b: number }>;
    (): IterableIterator<{ b: number }>;
} */
const result = arr[Symbol.iterator]().next();
if (!result.done) {
    result.value.a.toUpperCase(); // okay
    result.value.b.toFixed(); // error, expected this 
}
```


</details>


<details><summary><b>Compiler Options</b></summary>

```json
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}
```


</details>

**Playground Link:** [Provided](https://www.typescriptlang.org/play/?ts=4.0.0-beta&ssl=8&ssc=1&pln=3&pc=1#code/CYUwxgNghgTiAEYD2A7AzgF3rGAueAgjDFAJ4A8A3tvpjAJYoDm8AvgHzwBkhxZV8AEb4UAVwC2gkDDbsA3ACgFAMyQyAFMnRYQEEOICSGGUmXZiASniUF8eAHp71mvDqMWreAB9rg+CIkpGU9AXg3AVD3beF19IxgAOig4jCQAVQAHNOkAYSg0EHULOQcnaRg1AEJI6MNjOMEkpAAxegAPEGACosco4gqFTyVugyiWzLAMdqj6DAALaXhZ+jR4dUYJmDzx+lR4Uyi9cRAULAxSTLQLBS1MfZjgFvgAXnMYAG0ABgBdLqdqKFpjO4ip4eNRhPAxJJpMCFNUDPcEg10pkYDk8p1irsANZkWEHeEtOoNZptDqFTFIHGkQZONQLGZLFZIABu0ggSCgoGA8Gm0igyQ28CY9GZ7npCGU9A2OgORxOZwQqAgpEu1ywvJkzxwrwAyqRJEgIHENfy1N8FPYAFSIVA3DX4Gx2OwFfBGPmCPRukgCgT-VyA5iyRROlYWV3rKAekBe00wX0AhiBjiKEHWSLOsPwGNRmM+sEBKHBeTp0Ph92eiN5oQFoJB-rwS32K62rBwNCiCBYLXEXX6wSG42Vs0FOIoEAtDCdBT0MzqcptjsYOLAVAgKyO+ALztxZlQCCiECI5LI7K5fLk7qU3F2LdL3f7w-1ZIk9oY7qlNQAGhGYwm3MWyz9AoQA)