首页 > 解决方案 > 如何在 TypeScript 中声明由 for await 动态创建的对象的类型

问题描述

for await (account of accounts) { ... } 发出错误:“错误 TS2552:找不到名称‘帐户’。您的意思是‘帐户’吗?” 谢谢。

标签: typescriptfor-await

解决方案


我假设你有这样的东西:

const accounts = [1, 2, 3];

(async () => {
    for await (const account of accounts) { }
})()

在这种情况下,不需要显式键入accountconst,因为 TS 能够推断类型。

如果您仍想使用显式类型,您可以在以下之前声明您的变量for loop

const accounts: any[] = [1, 2, 3];

(async () => {
    let account: string;
    for await (account of accounts) { }
})()


推荐阅读