Skip to content

Commit

Permalink
fix(streams): reject string in ReadableStream.from type (#25116)
Browse files Browse the repository at this point in the history
WebIDL `async iterable<T>` type rejects `string`

Ref whatwg/webidl#1397, #24623
  • Loading branch information
petamoriken authored and bartlomieju committed Nov 28, 2024
1 parent 8cd9fa6 commit 2ee6b1a
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cli/tsc/dts/lib.dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18277,7 +18277,7 @@ declare var ReadableStream: {
new(underlyingSource: UnderlyingByteSource, strategy?: { highWaterMark?: number }): ReadableStream<Uint8Array>;
new<R = any>(underlyingSource: UnderlyingDefaultSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
new<R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
from<R>(asyncIterable: AsyncIterable<R> | Iterable<R | PromiseLike<R>>): ReadableStream<R>;
from<R>(asyncIterable: AsyncIterable<R> | Iterable<R | PromiseLike<R>> & object): ReadableStream<R>;
};

/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader) */
Expand Down
4 changes: 2 additions & 2 deletions ext/web/06_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -2922,7 +2922,7 @@ function readableStreamPipeTo(
}

/**
* @param {ReadableStreamGenericReader<any> | ReadableStreamBYOBReader} reader
* @param {ReadableStreamGenericReader | ReadableStreamBYOBReader} reader
* @param {any} reason
* @returns {Promise<void>}
*/
Expand Down Expand Up @@ -2955,7 +2955,7 @@ function readableStreamReaderGenericInitialize(reader, stream) {

/**
* @template R
* @param {ReadableStreamGenericReader<R> | ReadableStreamBYOBReader} reader
* @param {ReadableStreamGenericReader | ReadableStreamBYOBReader} reader
*/
function readableStreamReaderGenericRelease(reader) {
const stream = reader[_stream];
Expand Down
4 changes: 2 additions & 2 deletions ext/web/06_streams_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ interface VoidFunction {
(): void;
}

interface ReadableStreamGenericReader<T> {
readonly closed: Promise<void>;
interface ReadableStreamGenericReader {
readonly closed: Promise<undefined>;
// deno-lint-ignore no-explicit-any
cancel(reason?: any): Promise<void>;
}
Expand Down
2 changes: 1 addition & 1 deletion ext/web/lib.deno_web.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ declare var ReadableStream: {
strategy?: QueuingStrategy<R>,
): ReadableStream<R>;
from<R>(
asyncIterable: AsyncIterable<R> | Iterable<R | PromiseLike<R>>,
asyncIterable: AsyncIterable<R> | Iterable<R | PromiseLike<R>> & object,
): ReadableStream<R>;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"args": "check ./main.ts",
"output": "main.out",
"exitCode": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Check [WILDCARD]/main.ts
error: TS2345 [ERROR]: Argument of type 'string' is not assignable to parameter of type 'AsyncIterable<string> | (Iterable<string | PromiseLike<string>> & object)'.
ReadableStream.from("string");
~~~~~~~~
at [WILDCARD]/main.ts:1:21
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ReadableStream.from("string");
1 change: 1 addition & 0 deletions tests/unit/streams_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ Deno.test(async function decompressionStreamInvalidGzipStillReported() {

Deno.test(function readableStreamFromWithStringThrows() {
assertThrows(
// @ts-expect-error: primitives are not acceptable
() => ReadableStream.from("string"),
TypeError,
"Failed to execute 'ReadableStream.from': Argument 1 can not be converted to async iterable.",
Expand Down

0 comments on commit 2ee6b1a

Please sign in to comment.