microsoft / microsoft/TypeScript

Consider making `WritableStreamDefaultWriter.write()` contravariant

Offen
#59,012 2 Kommentare 4 Reaktionen 2 zugewiesene Personen Auf GitHub ansehen

@RyanCavanaugh arbeitet bereits daran.

Seit 19.9.2026.

  • #64339 von @copilot-swe-agent — offen
Bug Domain: lib.d.ts Help Wanted Needs Human Review
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

⚙ Compilation target

esnext

⚙ Library

dom

Missing / Incorrect Definition

WritableStreamDefaultWriter.write(chunk)

Sample Code
const test: WritableStream<{ a: string }> = new WritableStream<{ a: string; b: string }>();

The above does not raise a type error. test.getWriter().write({ a: "string" }) will not show an error, even though the underlying stream expects b properties on the chunks. A variation of the problem is this:

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

WritableStream<A> and WritableStream<B> differ in the signature of their getWriter().write(chunk) method. Because WritableStreamDefaultWriter.write(chunk) is a method, it is bivariant, leading to the above examples to pass.

Other than for example Array, WritableStream is only ever used for writing, never for reading, so as far as my understanding goes, bivariance does not have any use here. This is why I would suggest to change the typing of WritableStream to make it contravariant, to make the examples above raise errors. One way to achieve that would be to declare WritableStreamDefaultWriter.write() as a function property instead of a method, that would solve the problem at least in strict mode:

    write: (chunk?: W) => Promise<void>;

Although I am not sure whether that would have any other implications.

As a workaround, I use the following code:

declare const write: unique symbol;
declare global {
	interface WritableStreamDefaultWriter<W = any> {
		[write]?: (a: W) => void;
	}
}

Some more details about this can also be found on StackOverflow.

Documentation Link

No response

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.