类型 'IterableIterator<[number, any]>' 不是数组类型或字符串类型

pra*_*wal 7 typescript ionic4

我正在使用 ionic 4 来构建一个应用程序。而我的应用程序在浏览器上运行良好,但是当我构建 android 平台时,它给了我这一行的错误

for (let [index, p] of this.cart.entries())
Run Code Online (Sandbox Code Playgroud)

我的打字稿版本是 3.7.2

错误是

类型 'IterableIterator<[number, any]>' 不是数组类型或字符串类型。使用编译器选项“--downlevelIteration”来允许迭代器迭代。

pra*_*wal 11

我通过在 tsconfig.josn 文件中的 compilerOptions 中添加 "downlevelIteration": true 来解决它。注意我的目标是 es2015


Par*_*war 8

我遇到了类似的问题,以下是地图和循环出现错误

public lastviewed = new Map<string, object>();

for (const [key, value] of this.lastviewed.entries()) { //didnt work
    console.log(" Key : ", key, " Val : ", value);
}
Run Code Online (Sandbox Code Playgroud)

导致错误:类型“IterableIterator<[string, object]>”不是数组类型或字符串类型。使用编译器选项“--downlevelIteration”而不是在 tsconfig 中进行更改,我将循环从条目更改为forEach,它对我来说很有效

    this.lastviewed.forEach((value: object, key: string) => {//worked
        console.log("PRINTING : ", key, value);
    });
Run Code Online (Sandbox Code Playgroud)

感谢thefourye的回答