TypeScript Cannot Auto-Infer Async Generator Input Types, Developers Must Annotate

TypeScript's async generator model relies on three explicit type parameters — T for yielded values, TReturn for the final return value, and TNext for values sent back via .next() — but the compiler cannot infer TNext automatically. This limitation exists because yield expressions are bidirectional: a generator yields values outward while callers can simultaneously send values back through the same yield point, creating an inference deadlock. Without explicit annotations, TypeScript defaults to AsyncGenerator<any, any, undefined>, leaving type safety gaps at critical boundaries in streaming, pagination, or event-processing code. Unlike regular functions where parameter types can be inferred from usage, async generators receive their 'inputs' asynchronously through an external iteration protocol, breaking standard inference patterns. Developers must therefore manually annotate all three type parameters whenever a generator consumes input through yield to ensure proper type safety across the codebase.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in