Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tsc/internal/bundled/libs/lib.dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41916,7 +41916,7 @@ interface WritableStreamDefaultWriter<W = any> {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/write)
*/
write(chunk?: W): Promise<void>;
write: (chunk?: W) => Promise<void>;
}

declare var WritableStreamDefaultWriter: {
Expand Down
2 changes: 1 addition & 1 deletion tsc/internal/bundled/libs/lib.webworker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14159,7 +14159,7 @@ interface WritableStreamDefaultWriter<W = any> {
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter/write)
*/
write(chunk?: W): Promise<void>;
write: (chunk?: W) => Promise<void>;
}

declare var WritableStreamDefaultWriter: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
writableStreamChunkVariance.ts(1,7): error TS2322: Type 'WritableStream<{ a: string; b: string; }>' is not assignable to type 'WritableStream<{ a: string; }>'.
Property 'b' is missing in type '{ a: string; }' but required in type '{ a: string; b: string; }'.
writableStreamChunkVariance.ts(5,44): error TS2345: Argument of type 'WritableStream<{ a: string; b: string; }>' is not assignable to parameter of type 'WritableStream<{ a: string; }>'.
Property 'b' is missing in type '{ a: string; }' but required in type '{ a: string; b: string; }'.


==== writableStreamChunkVariance.ts (2 errors) ====
const stream: WritableStream<{ a: string }> = new WritableStream<{ a: string; b: string }>();
~~~~~~
!!! error TS2322: Type 'WritableStream<{ a: string; b: string; }>' is not assignable to type 'WritableStream<{ a: string; }>'.
!!! error TS2322: Property 'b' is missing in type '{ a: string; }' but required in type '{ a: string; b: string; }'.
!!! related TS2728 writableStreamChunkVariance.ts:1:79: 'b' is declared here.
stream.getWriter().write({ a: "string" });

const writable = new WritableStream<{ a: string; b: string }>();
new ReadableStream<{ a: string }>().pipeTo(writable);
~~~~~~~~
!!! error TS2345: Argument of type 'WritableStream<{ a: string; b: string; }>' is not assignable to parameter of type 'WritableStream<{ a: string; }>'.
!!! error TS2345: Property 'b' is missing in type '{ a: string; }' but required in type '{ a: string; b: string; }'.
!!! related TS2728 writableStreamChunkVariance.ts:4:50: 'b' is declared here.

const widened: WritableStream<{ a: string; b: string }> = new WritableStream<{ a: string }>();
new ReadableStream<{ a: string; b: string }>().pipeTo(new WritableStream<{ a: string }>());

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/writableStreamChunkVariance.ts] ////

//// [writableStreamChunkVariance.ts]
const stream: WritableStream<{ a: string }> = new WritableStream<{ a: string; b: string }>();
stream.getWriter().write({ a: "string" });

const writable = new WritableStream<{ a: string; b: string }>();
new ReadableStream<{ a: string }>().pipeTo(writable);

const widened: WritableStream<{ a: string; b: string }> = new WritableStream<{ a: string }>();
new ReadableStream<{ a: string; b: string }>().pipeTo(new WritableStream<{ a: string }>());


//// [writableStreamChunkVariance.js]
"use strict";
const stream = new WritableStream();
stream.getWriter().write({ a: "string" });
const writable = new WritableStream();
new ReadableStream().pipeTo(writable);
const widened = new WritableStream();
new ReadableStream().pipeTo(new WritableStream());
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//// [tests/cases/compiler/writableStreamChunkVariance.ts] ////

=== writableStreamChunkVariance.ts ===
const stream: WritableStream<{ a: string }> = new WritableStream<{ a: string; b: string }>();
>stream : Symbol(stream, Decl(writableStreamChunkVariance.ts, 0, 5))
>WritableStream : Symbol(WritableStream, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 0, 30))
>WritableStream : Symbol(WritableStream, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 0, 66))
>b : Symbol(b, Decl(writableStreamChunkVariance.ts, 0, 77))

stream.getWriter().write({ a: "string" });
>stream.getWriter().write : Symbol(WritableStreamDefaultWriter.write, Decl(lib.dom.d.ts, --, --))
>stream.getWriter : Symbol(WritableStream.getWriter, Decl(lib.dom.d.ts, --, --))
>stream : Symbol(stream, Decl(writableStreamChunkVariance.ts, 0, 5))
>getWriter : Symbol(WritableStream.getWriter, Decl(lib.dom.d.ts, --, --))
>write : Symbol(WritableStreamDefaultWriter.write, Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 1, 26))

const writable = new WritableStream<{ a: string; b: string }>();
>writable : Symbol(writable, Decl(writableStreamChunkVariance.ts, 3, 5))
>WritableStream : Symbol(WritableStream, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 3, 37))
>b : Symbol(b, Decl(writableStreamChunkVariance.ts, 3, 48))

new ReadableStream<{ a: string }>().pipeTo(writable);
>new ReadableStream<{ a: string }>().pipeTo : Symbol(ReadableStream.pipeTo, Decl(lib.dom.d.ts, --, --))
>ReadableStream : Symbol(ReadableStream, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 4, 20))
>pipeTo : Symbol(ReadableStream.pipeTo, Decl(lib.dom.d.ts, --, --))
>writable : Symbol(writable, Decl(writableStreamChunkVariance.ts, 3, 5))

const widened: WritableStream<{ a: string; b: string }> = new WritableStream<{ a: string }>();
>widened : Symbol(widened, Decl(writableStreamChunkVariance.ts, 6, 5))
>WritableStream : Symbol(WritableStream, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 6, 31))
>b : Symbol(b, Decl(writableStreamChunkVariance.ts, 6, 42))
>WritableStream : Symbol(WritableStream, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 6, 78))

new ReadableStream<{ a: string; b: string }>().pipeTo(new WritableStream<{ a: string }>());
>new ReadableStream<{ a: string; b: string }>().pipeTo : Symbol(ReadableStream.pipeTo, Decl(lib.dom.d.ts, --, --))
>ReadableStream : Symbol(ReadableStream, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 7, 20))
>b : Symbol(b, Decl(writableStreamChunkVariance.ts, 7, 31))
>pipeTo : Symbol(ReadableStream.pipeTo, Decl(lib.dom.d.ts, --, --))
>WritableStream : Symbol(WritableStream, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>a : Symbol(a, Decl(writableStreamChunkVariance.ts, 7, 74))

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//// [tests/cases/compiler/writableStreamChunkVariance.ts] ////

=== writableStreamChunkVariance.ts ===
const stream: WritableStream<{ a: string }> = new WritableStream<{ a: string; b: string }>();
>stream : WritableStream<{ a: string; }>
>a : string
>new WritableStream<{ a: string; b: string }>() : WritableStream<{ a: string; b: string; }>
>WritableStream : { new <W = any>(underlyingSink?: UnderlyingSink<W>, strategy?: QueuingStrategy<W>): WritableStream<W>; prototype: WritableStream; }
>a : string
>b : string

stream.getWriter().write({ a: "string" });
>stream.getWriter().write({ a: "string" }) : Promise<void>
>stream.getWriter().write : (chunk?: { a: string; } | undefined) => Promise<void>
>stream.getWriter() : WritableStreamDefaultWriter<{ a: string; }>
>stream.getWriter : () => WritableStreamDefaultWriter<{ a: string; }>
>stream : WritableStream<{ a: string; }>
>getWriter : () => WritableStreamDefaultWriter<{ a: string; }>
>write : (chunk?: { a: string; } | undefined) => Promise<void>
>{ a: "string" } : { a: string; }
>a : string
>"string" : "string"

const writable = new WritableStream<{ a: string; b: string }>();
>writable : WritableStream<{ a: string; b: string; }>
>new WritableStream<{ a: string; b: string }>() : WritableStream<{ a: string; b: string; }>
>WritableStream : { new <W = any>(underlyingSink?: UnderlyingSink<W>, strategy?: QueuingStrategy<W>): WritableStream<W>; prototype: WritableStream; }
>a : string
>b : string

new ReadableStream<{ a: string }>().pipeTo(writable);
>new ReadableStream<{ a: string }>().pipeTo(writable) : Promise<void>
>new ReadableStream<{ a: string }>().pipeTo : (destination: WritableStream<{ a: string; }>, options?: StreamPipeOptions) => Promise<void>
>new ReadableStream<{ a: string }>() : ReadableStream<{ a: string; }>
>ReadableStream : { new (underlyingSource: UnderlyingByteSource, strategy?: { highWaterMark?: number; }): ReadableStream<Uint8Array<ArrayBuffer>>; new <R = any>(underlyingSource: UnderlyingDefaultSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>; new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>; prototype: ReadableStream; }
>a : string
>pipeTo : (destination: WritableStream<{ a: string; }>, options?: StreamPipeOptions) => Promise<void>
>writable : WritableStream<{ a: string; b: string; }>

const widened: WritableStream<{ a: string; b: string }> = new WritableStream<{ a: string }>();
>widened : WritableStream<{ a: string; b: string; }>
>a : string
>b : string
>new WritableStream<{ a: string }>() : WritableStream<{ a: string; }>
>WritableStream : { new <W = any>(underlyingSink?: UnderlyingSink<W>, strategy?: QueuingStrategy<W>): WritableStream<W>; prototype: WritableStream; }
>a : string

new ReadableStream<{ a: string; b: string }>().pipeTo(new WritableStream<{ a: string }>());
>new ReadableStream<{ a: string; b: string }>().pipeTo(new WritableStream<{ a: string }>()) : Promise<void>
>new ReadableStream<{ a: string; b: string }>().pipeTo : (destination: WritableStream<{ a: string; b: string; }>, options?: StreamPipeOptions) => Promise<void>
>new ReadableStream<{ a: string; b: string }>() : ReadableStream<{ a: string; b: string; }>
>ReadableStream : { new (underlyingSource: UnderlyingByteSource, strategy?: { highWaterMark?: number; }): ReadableStream<Uint8Array<ArrayBuffer>>; new <R = any>(underlyingSource: UnderlyingDefaultSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>; new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>; prototype: ReadableStream; }
>a : string
>b : string
>pipeTo : (destination: WritableStream<{ a: string; b: string; }>, options?: StreamPipeOptions) => Promise<void>
>new WritableStream<{ a: string }>() : WritableStream<{ a: string; }>
>WritableStream : { new <W = any>(underlyingSink?: UnderlyingSink<W>, strategy?: QueuingStrategy<W>): WritableStream<W>; prototype: WritableStream; }
>a : string

12 changes: 12 additions & 0 deletions tsc/testdata/tests/cases/compiler/writableStreamChunkVariance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @strict: true
// @target: esnext
// @lib: esnext,dom

const stream: WritableStream<{ a: string }> = new WritableStream<{ a: string; b: string }>();
stream.getWriter().write({ a: "string" });

const writable = new WritableStream<{ a: string; b: string }>();
new ReadableStream<{ a: string }>().pipeTo(writable);

const widened: WritableStream<{ a: string; b: string }> = new WritableStream<{ a: string }>();
new ReadableStream<{ a: string; b: string }>().pipeTo(new WritableStream<{ a: string }>());