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

Readable Stream not being identified as iterable in CommonJS #39051

Closed
KanagawaMarcos opened this issue Jun 12, 2020 · 7 comments
Closed

Readable Stream not being identified as iterable in CommonJS #39051

KanagawaMarcos opened this issue Jun 12, 2020 · 7 comments

Comments

@KanagawaMarcos
Copy link

TypeScript Version: 4.0.0-dev.20200611

Search Terms:
Async Iterator, Symbol, Readable Stream, CommonJS

Expected behavior:

Typescript should not give any errors since Readable Streams in node supports async iterators.

Actual behavior:

Type 'ReadableStream<any>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.

Related Issues:

Code

export async function foo (file: ReadableStream) {
    for await (const chunk of file) {
        console.log(chunk)
    }
}

Workaround

export async function foo (file: Iterable<ReadableStream>) {
    for await (const chunk of file) {
        console.log(chunk)
    }
}
Output
var __asyncValues = (this && this.__asyncValues) || function (o) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var m = o[Symbol.asyncIterator], i;
    return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
    function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
    function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
export async function foo(file) {
    var e_1, _a;
    try {
        for (var file_1 = __asyncValues(file), file_1_1; file_1_1 = await file_1.next(), !file_1_1.done;) {
            const chunk = file_1_1.value;
            console.log(chunk);
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (file_1_1 && !file_1_1.done && (_a = file_1.return)) await _a.call(file_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "CommonJS"
  }
}

Playground Link: Provided

@TrySound
Copy link

TrySound commented May 1, 2023

Was it fixed?

@limdauto
Copy link

limdauto commented Jun 6, 2023

I'm still seeing this error. Could we reopen please?

@RyanCavanaugh
Copy link
Member

ReadableStream is defined in the Node .d.ts file on DefinitelyTyped; it'd have to be changed there

@ColemanDunn
Copy link

ReadableStream is defined in the Node .d.ts file on DefinitelyTyped; it'd have to be changed there

@RyanCavanaugh does this mean this issue should be opened again but elsewhere? The fact that copying and pasting the example found at https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#async_iteration results in a compilation error seems like a problem.

@ezra-en
Copy link

ezra-en commented Jun 21, 2023

It seems that the problem is being discussed (DefinitelyTyped/DefinitelyTyped#62651), but not much else.

Related discussion: DefinitelyTyped/DefinitelyTyped#65542 (comment)

@LorenzoBloedow
Copy link

Solution if you're using Node.js:

// this
let stream: NodeJS.ReadableStream

// instead of this
let stream: ReadableStream

@GustavoOS
Copy link

let stream: NodeJS.ReadableStream

How is the import?

@microsoft microsoft locked as resolved and limited conversation to collaborators Feb 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants