-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: IntersectionIncorrect behavior under `T & U` constructsIncorrect behavior under `T & U` constructsHelp WantedYou can do thisYou can do this
Milestone
Description
🔎 Search Terms
AsyncIterablefor await
🕗 Version & Regression Information
- This changed between versions N/A and N/A
- This changed in commit or PR N/A
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
- I was unable to test this on prior versions because _______
⏯ Playground Link
💻 Code
type Stream<T_Sync, T_Async> = Iterable<T_Sync> & AsyncIterable<T_Async>
class A {};
class B {};
async function loop(stream: Stream<A, B>) {
// ok: variable `a` is of type `A`
for (const a of stream) {}
// Bad: variable `b` should be of type `B`, TS asserted `A`
for await (const b of stream) {}
}🙁 Actual behavior
TS asserts b of type A even if stream implements AsyncIterable<B>.
🙂 Expected behavior
b should be asserted to type B.
Additional information about the issue
Related issue #36153 discussed adding a type helper ForAwaitable for types support either sync or async iteration. However, the OP does not cover the issue that TS should prefer AsyncIterable<T> in async for loop when possible.
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: IntersectionIncorrect behavior under `T & U` constructsIncorrect behavior under `T & U` constructsHelp WantedYou can do thisYou can do this