-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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. #36687
Comments
Shorter repro interface Foo {
id : string,
}
declare function getFoo(after: string|undefined): Promise<Foo>;
async function main() {
let after: string | undefined = undefined;
while (true) {
const foo = await getFoo(after);
after = foo.id;
}
} |
OK. I think this is an issue of type narrowing. It looks to me like the first time the loop runs, |
the example here would be broken even if we didn't have this issue because |
Here's a version of this code that does work (I am not sure why, maybe because not explicitly defining interface Foo {
id : string,
}
declare function paginate(after: string|undefined): Promise<Foo[]>;
async function main() {
let after: string | undefined;
while (true) {
const page = await paginate(after);
for (const foo of page) {
after = foo.id;
}
}
} smaller repro: declare function paginate(after: string|undefined): Promise<string>;
async function main() {
let after: string | undefined = undefined;
while (true) {
const page = await paginate(after);
after = page?.[0]
}
} The following things stop the error:
Things that don't stop the error:
|
What are you talking about? |
sorry i misread 😅 |
This is a legit circularity and none of our existing circularity-shortcutting mechanisms work in this case |
How is it circular, despite the explicit type annotation, and explicit initialization? Why would removing explicit initialization ( If it was circular before removing it, it should be circular after removing it |
Still confused here =( |
Bump to prevent auto closing |
Any update on this? Just faced this very error in the |
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 |
I just ran into this issue with a very similar scenario. Was very surprised to find this as an open issue still... and originating in 2020... I can't share the complete code here, but the short of it is essentially: async processStream(): Promise<void> {
let ShardIterator: string | undefined = await this.getShardIterator() // () => Promise<string>
while (ShardIterator) {
const res = await this.ddbStreamsClient.getRecords({ ShardIterator, Limit: 10 }).promise()
// ^ res has implicit any
// ... other code ...
ShardIterator = res.NextShardIterator // string | undefined
}
} outside of the loop block this isn't an issue. This is just my first stop in my search for an answer, but have not seen anything in the responses here that makes me feel like error is by design. |
Just got this on a do..while loop using pagination with a cursor, feels odd that this triggers a problem on TS. |
Any update on this? It's very annoying to use workarounds, cmon |
Update? |
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 |
Still not working for me with TypeScript 5.3.3. |
Still ran into this issue in one of my code. Everything was working fine if I typed the result of Why closing the issue on Feb 24, while a comment on Feb 7 was issued? Very puzzling management |
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
Expected behavior:
Type checks successfully
Actual behavior:
![image](https://user-images.githubusercontent.com/5655961/74070095-05f11e00-49ce-11ea-9340-d2cec4e3a276.png)
Playground Link: Playground
Related Issues:
Similar to #14428 , an already fixed issue.
Similar to #36666 , but without destructuring; and my initial variable also has explicit type annotations (
after : string|undefined
)Workaround
Just use an explicit type annotation,
However, it is strange. It shouldn't need the explicit type annotation because it can only possibly be
Foo[]
.The text was updated successfully, but these errors were encountered: