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

https://typescript-eslint.io/play/#ts=5.4.3&showAST=es&fileType=.tsx&code=MYewdgzgLgBAhhAnmYBJKBTATgLhgQSRXWzgCMAbDAHmiwEswBzAPhgB8YStyraoGzNgF4YAbQC6AbgBQAMxBZ4Adzj1YAClCRYEGCDnwiaTFgCUMAN4wAvjIwAPAA6LY1m0A&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6AQwHd3K78ALRE3YAjJOiiJo0APbRI4MAF8QSoA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

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:

  1. No lint error, because the code is arguably correct (for await works on both sync and async iterables)
  2. 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 await because 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)

Most upvoted comments

My code uses Iterable<T> | AsyncIterable<T> for the same reason as T | Promise<T>: some implementations might be sync, the union type avoids unnecessary async keywords (and lint errors) on them.

From reading the above comments, what I understand is that the control flows of Iterable<Promise<T>> and AsyncIterable<T> are different. But in my usage T is a concrete type (and I guess in generic usages, T would have a type constraint), so our functions will only accept Iterable<T> and AsyncIterable<T>, but not Iterable<Promise<T>>, thus the problem won’t happen.

So, the only difference between for await ... of value blindly and Symbol.asyncIterator in value ? for await ... of value : for ... of value is the extra microtasks, which I also don’t care.

Hello I just opened a PR to fix this issue,

However I don’t know if that will resolve it for your example

const asyncIter: AsyncIterable<string> | Iterable<string> = [];
for await (const s of asyncIter) { }

I think Typescript will automatically deduct that asyncIter is only an array, and the variable won’t be treated as a Union type…

Yeah, that’s just TS being clever and narowing asyncIter based off its actual value, rather than treating it as the type in the type annotation. Technically we could write the repro more correctly as

declare const asyncIter: AsyncIterable<string> | Iterable<string>;

for await (const s of asyncIter) { }

export { }

or

async function foo(asyncIter: AsyncIterable<string> | Iterable<string>) {
  for await (const s of asyncIter) { }
}

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:

const result = (value instanceof Promise) ? await value : value;

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 uses for await to handle both cases without repeating code. But I can disable the lint rule in that case as mentioned in the docs.