Skip to content

Support async iterable DOM types #29867

Closed
@MattiasBuelens

Description

@MattiasBuelens

Search Terms

asynciterator asynciterable async iterable iterator

Suggestion

Add a dom.asynciterable option to --lib to support DOM types that define a Symbol.asyncIterator method.

Use Cases

The streams spec has recently been updated to add a Symbol.asyncIterator method to ReadableStream, making it the first DOM type to implement the ES2018 AsyncIterable interface.

I would like to update the type definitions for streams in TSJS-lib-generator to include this new async iterator method. However, this requires Symbol.asyncIterator, which is not available before ES2018 and thus cannot be readily used in the DOM type definitions.

Previously, a dom.iterable option was added to support DOM types that define a Symbol.iterator method. I think it would make sense to use this same strategy to support Symbol.asyncIterator.

Examples

TypeScript would have (generated) type definitions in lib/lib.dom.asynciterable.d.ts:

interface ReadableStream<R = any> {
  [Symbol.asyncIterator](): AsyncIterator<R>;
}

Users could then use a ReadableStream in a for await..of loop by compiling with --target es2018:

const readable = new ReadableStream<Uint8Array>();
for await (const chunk of readable) {
  console.log(chunk);
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions