TypeScript: async, await, loop, implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
TypeScript Version: 3.5.1, 3.7.5, 3.8-Beta
Search Terms: async, await, loop, promise, variable assignment, implicitly has type any, referenced directly or indirectly in its own initializer.
Code
interface Foo {
id : string,
}
declare function paginate(after: string|undefined): Promise<Foo[]>;
async function main() {
let after: string | undefined = undefined;
while (true) {
const page = await paginate(after);
for (const foo of page) {
after = foo.id;
}
}
}
Expected behavior:
Type checks successfully
Actual behavior:

Playground Link: Playground
Related Issues:
Similar to https://github.com/microsoft/TypeScript/issues/14428 , an already fixed issue.
Similar to https://github.com/microsoft/TypeScript/issues/36666 , but without destructuring; and my initial variable also has explicit type annotations (after : string|undefined)
Workaround
Just use an explicit type annotation,
const page : Foo[] = await paginate(after);
However, it is strange. It shouldn’t need the explicit type annotation because it can only possibly be Foo[].
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 19
- Comments: 18 (6 by maintainers)
Any update on this? Just faced this very error in the
whileloop.This is a legit circularity and none of our existing circularity-shortcutting mechanisms work in this case
I think this issue might be fixed since I don’t get any errors in the playground when I choose ts 5.
However, I have also run into this or a similar issue and just wanted to share what helped me. And that is to explicitly type the awaited result which would be
const pagein @AnyhowStep example.Update?
Faced this again, found this thread again, realized that I had already commented on this a year ago.
Well, as I’m back here, any update on this? Removing the
async/awaitstuff from this throws no error, so I believe it’s more like a bug than a design limitation.