js map 循环
发布于 3 年前 作者 gocpplua 2389 次浏览 来自 分享
// --target es6
// error TS2569: Type ‘IterableIterator<string>’ is not an array type or a string type. Use compiler option ‘–downlevelIteration’ to allow iterating of iterators.
let map= new Map<string, string>();
map.set("a1","1");
map.set("a2","2");
map.set("a3","3");
map.forEach((value , key) =>{
if(value == 'a1'){
return;
}
console.log(key) // output: a1 a2 a3
});

for (const i of map.values()) {
if(i == '1'){
break;
}
console.log(i) // not run here
}
1 回复

error TS2569: Type ’ IterableIterator<string>’ is not an array type or a string type. Use compiler option ‘–downlevelIteration’ to allow iterating of iterators.

请检查你的TypeScript的target是不是es6?如果是es6以下版本,请这样做(在ts.config):

{
  "compilerOptions": {
    /* Basic Options */
     "downlevelIteration": true,
/* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
}
回到顶部