Skip to content

Error message on use of async iterators in nodeJS 10 streams misleading #26677

@BurtHarris

Description

@BurtHarris

TypeScript Version: typescript@3.1.0-dev.20180825

Search Terms: async iterator ES2018

Code

import { PassThrough } from "stream";  // with  @types/node@10.9.2
// Compiled with --target ES2018.

const stream = new PassThrough();
stream.write( Buffer.from([0,1,2,3]));

async function doIt( )  {
    var buf : Buffer;
    for await (  buf of stream ) {
        console.trace('>>>buf ',  buf);
    }
}

doIt();

Expected behavior:

No error message. Fallback code generation or polyfill based Symbol.asyncIterator.

Actual behavior:

Message: message index.ts:12:25 - error TS2519: An async iterator must have a 'next()' method.

This error message is wrong, because the code generated (__asyncValues) depends on [Symbol.asyncIterator] rather than next() on the stream.

Playground Link: Playground does not behave the same, it currently generates different code that actually does depend on a next() method, so this link is not particularly useful.

Related Issues: #20463, the fix here might make the message go away, just as using --target ESNEXT does, but I think that misses the point. Shouldn't the --target option control the choice of generated language, and not what input syntax is permitted?

This currently happens for If --target ES2015, ES2016, ES2017, or ES2018, but not ESNEXT. If --target ES5, a different error is generated, requiring a Promise constructor.

I'm hoping we should be able to polyfill async iterators into any implementation support promises and symbols.,

Note: the of the @types/node@10.9.2 version of index.d.ts I'm using does declare that stream has a [Symbol.asyncIterator] method. If it lacked that, then the message displayed might make sense, but as the generated code never even uses next() method, the message is still confusing..

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions