js map 循环
// --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'. */
}