typescript - 如何在 TypeScript 中声明由 for await 动态创建的对象的类型
问题描述
for await (account of accounts) { ... }
发出错误:“错误 TS2552:找不到名称‘帐户’。您的意思是‘帐户’吗?” 谢谢。
解决方案
我假设你有这样的东西:
const accounts = [1, 2, 3];
(async () => {
for await (const account of accounts) { }
})()
在这种情况下,不需要显式键入account
const,因为 TS 能够推断类型。
如果您仍想使用显式类型,您可以在以下之前声明您的变量for loop
:
const accounts: any[] = [1, 2, 3];
(async () => {
let account: string;
for await (account of accounts) { }
})()
推荐阅读
- javascript - 有时会:无法在“FileReader”上执行“readAsDataURL”:参数 1 不是“Blob”类型
- c# - 无法在 AspNetCore 3.1 的视图中注入扩展 IdentityUser
- autodesk-forge - 有没有办法在 Forge Viewer 的模型浏览器中获取 Navis Works 的选择树下拉菜单
- python - 未检测到的 Chromedriver 未正确加载
- oauth-2.0 - 为 Xero API 构建 Power BI 连接器
- c# - 为什么第一列在c#的控制台窗口上保持为空
- android - 在 Fragments Kotlin 中隐藏或显示操作栏
- android - 深度缓冲区在 android 上不起作用 Opengl es
- php - 如何在 mysql 查询中组合多个表并根据列的总数进行排名
- firebase - 关于 Firebase Rest Api 加入操作