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

ReadableStream.from() method? #1691

Open
karlhorky opened this issue Feb 17, 2024 · 1 comment
Open

ReadableStream.from() method? #1691

karlhorky opened this issue Feb 17, 2024 · 1 comment

Comments

@karlhorky
Copy link

karlhorky commented Feb 17, 2024

Hi, first of all, thanks for your work on these type declarations, they are invaluable for working with web standards APIs.

It seems as if the following is not yet supported in TS 5.3.3 (also fails in Beta and Nightly):

ReadableStream.from(['a', 'b']);
//             ^^^^? Property 'from' does not exist on type '{
//                     new (body?: BodyInit | null | undefined, init?: ResponseInit | undefined): Response;
//                     prototype: Response;
//                     error(): Response;
//                     json(data: any, init?: ResponseInit | undefined): Response;
//                     redirect(url: string | URL, status?: number | undefined): Response;
//                   }'.

Maybe a similar PR is needed to update the types to the WebIDL Streams spec like @MattiasBuelens did in 2020 in #890?

Or is this not supported yet because only 3 runtimes (Firefox, Deno and Node.js) support this?

I can also re-submit this in https://github.com/microsoft/typescript if that makes more sense.

@MattiasBuelens
Copy link
Contributor

I think you meant ReadableStream.from() in your example code, rather than Response.from(). 😉

Or is this not supported yet because only 3 runtimes (Firefox, Deno and Node.js) support this?

Correct, this is still missing browser support. At the moment, only 1 browser engine (Firefox's Gecko engine) has implemented ReadableStream.from(). (Node.js and Deno don't count, since they aren't browser engines.)

Usually, I would suggest to add a type definition to your own code for now. Unfortunately, that doesn't work in this case, since you can't override the type of declare var ReadableStream (playground)... 😕

Off topic

For the JavaScript types, we usually do things like:

interface Array {
  // instance properties and methods
}
interface ArrayConstructor {
  // constructors, static properties and methods
}
declare var Array: ArrayConstructor;

That way, users can augment ArrayConstructor in their own code, and have extra static Array methods that way. Unfortunately, we don't do this for any of the DOM types... (Presumably because it'd add a ton of extra types?)

So for now, you have to add an explicit cast wherever you want to use the extra methods... (playground)

type ReadableStreamExt = (typeof ReadableStream) & {
    from<R>(asyncIterable: Iterable<R> | AsyncIterable<R>): ReadableStream<R>;
};

// 😬
(ReadableStream as ReadableStreamExt).from(['a', 'b']);

You can also go click the "+1" button on the Chromium issue, so maybe someone will pick it up. 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants