How to declare type of an object created dinamicaly by for await in TypeScript

103 Views Asked by At

for await (account of accounts) { ... } Emit an error: "error TS2552: Cannot find name 'account'. Did you mean 'accounts'?" Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

I assume you have smth like that:

const accounts = [1, 2, 3];

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

In this case, there is no need to explicitly type account const because TS is able to infer the type.

If you still want to use explicit type, you can declare your variable before the for loop:

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

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