google / google/closure-compiler
Type inference when iterating over maps could be smarter
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
Since Map is a built in, I think the below will be a common enough problem to warrant a bit of special casing. It would be nice if the type system could know that Map[Symbol.iterator].next().value is an array of [A, B].
``` js
/** @const {!Map} */
const map = new Map();
for (let [x, y] of map) {
/** @type {string} */
let a = x;
/** @type {number} */
let b = y;
}
```
It currently results in these compiler warnings:
```
JSC_TYPE_MISMATCH: initializing variable
found : (number|string)
required: string at line 6 character 10
let a = x;
^
JSC_TYPE_MISMATCH: initializing variable
found : (number|string)
required: number at line 8 character 10
let b = y;
^
```
Contributor guide
Assessment
This issue has not been assessed yet.