microsoft / microsoft/TypeScript

Consider making `WritableStreamDefaultWriter.write()` contravariant

Ouverte
#59,012 2 commentaires 4 réactions 2 personnes assignées Voir sur GitHub

@RyanCavanaugh y travaille déjà.

Depuis le 19/9/2026.

  • #64339 par @copilot-swe-agent — ouverte
Bug Domain: lib.d.ts Help Wanted Needs Human Review
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

⚙ 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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.