Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
BurtHarris opened this issue Aug 25, 2018 · 3 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript

Comments

@BurtHarris
Copy link

BurtHarris commented Aug 25, 2018

TypeScript Version: [email protected]

Search Terms: async iterator ES2018

Code

import { PassThrough } from "stream";  // with  @types/[email protected]
// 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/[email protected] 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..

@ghost
Copy link

ghost commented Aug 27, 2018

It looks like Stream.prototype[Symbol.asyncIterator] returns a AsyncIterableIterator which is not defined under the default lib setting for es2018. Node provides a stub declaration, but that doesn't have next() in it. Using --target es2018 --lib esnext works, but if we support compiling to async iterators presumably we should have the interface available too.

@ghost ghost added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Aug 27, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Oct 9, 2018
@rbuckton
Copy link
Member

This is analogous to defining a custom default lib and not filling in the type for Promise and getting errors for await not working.

We should be able to resolve this by changing the node definitions to use lib references once typesVersions support is rolled out to DefinitelyTyped.

@rbuckton
Copy link
Member

This should be resolved now that the NodeJS definitions on DefinitelyTyped are using "typesVersions" and lib references.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants