如何在 TypeScript 中声明由 for await 动态创建的对象的类型
How to declare type of an object created dinamicaly by for await in TypeScript
for await (account of accounts) { ... }
发出错误:“错误 TS2552:找不到名称 'account'。你是说 'accounts' 吗?”
谢谢
我假设你有这样的事情:
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) { }
})()
for await (account of accounts) { ... }
发出错误:“错误 TS2552:找不到名称 'account'。你是说 'accounts' 吗?”
谢谢
我假设你有这样的事情:
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) { }
})()