typescript-eslint: Bug: await-thenable doesn't handle unions cleanly in for-await [8.8.0 regression]
Before You File a Bug Report Please Confirm You Have Done The Following…
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I have searched for related issues and found none that matched my issue.
- I have read the FAQ and my problem is not listed.
Playground Link
Repro Code
const asyncIter: AsyncIterable<string> | Iterable<string> = [];
for await (const s of asyncIter) { }
export { }
ESLint Config
See playground
tsconfig
{
"compilerOptions": {
// ...
}
}
Expected Result
If I call for await on an iterator that is typed as AsyncIterable<string> | Iterable<string>, I expect one of the following to happen:
- No lint error, because the code is arguably correct (
for awaitworks on both sync and async iterables) - Lint error but no autofix, because the code is arguably ugly (taking a union of sync and async iterables leads to unnecessary promises being created, which is the whole point of this rule), but it’s not safe to remove the
awaitbecause that would introduce a code bug.
Actual Result
Lint error, with an unsafe autofix suggesting to remove the await.
Additional Info
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 15 (14 by maintainers)
My code uses
Iterable<T> | AsyncIterable<T>for the same reason asT | Promise<T>: some implementations might be sync, the union type avoids unnecessaryasynckeywords (and lint errors) on them.From reading the above comments, what I understand is that the control flows of
Iterable<Promise<T>>andAsyncIterable<T>are different. But in my usageTis a concrete type (and I guess in generic usages,Twould have a type constraint), so our functions will only acceptIterable<T>andAsyncIterable<T>, but notIterable<Promise<T>>, thus the problem won’t happen.So, the only difference between
for await ... of valueblindly andSymbol.asyncIterator in value ? for await ... of value : for ... of valueis the extra microtasks, which I also don’t care.Yeah, that’s just TS being clever and narowing
asyncIterbased off its actual value, rather than treating it as the type in the type annotation. Technically we could write the repro more correctly asor
Yeah I would be looking at it from the perspective that if you’re attempting to iterate a
AsyncIterable<string> | Iterable<string>then there is no option for you to separate out those types, right?You could probably fashion some code that allows you to split things into two loops - one that’s async and one that’s not. But my question would be - why would you do that when that would require you to duplicate code.
Its the same reason we don’t force people to do this for the regular promise union case:
Sure this code is “more correct” but it’s also not any better or any clearer.
So yeah I’d argue that from a DevX perspective flagging this is technically correct, but realistically it’s fine to await this specific union case.
Ah sorry you’re right, it is a suggestion, not an autofix.
Closing; it looks like everything here is working as intended.
My use case is that I have some library code that intentionally takes in a parameter typed
AsyncIterable<T> | Iterable<T>and usesfor awaitto handle both cases without repeating code. But I can disable the lint rule in that case as mentioned in the docs.