Skip to content

Make WritableStreamDefaultWriter.write contravariant in its chunk type - #64339

Draft
Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 3 commits into
mainfrom
copilot/consider-making-writablestream-default-writer-cont
Draft

Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 3 commits into
mainfrom
copilot/consider-making-writablestream-default-writer-cont

Conversation

Copilot AI commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

WritableStreamDefaultWriter<W> declared write as a method, so its parameter was compared bivariantly. W reaches WritableStream<W> only through getWriter(), so the entire stream type was bivariant in its chunk type, accepting unsound assignments:

const stream: WritableStream<{ a: string }> = new WritableStream<{ a: string; b: string }>();
stream.getWriter().write({ a: "string" }); // no error, but the sink requires `b`

new ReadableStream<{ a: string }>().pipeTo(new WritableStream<{ a: string; b: string }>()); // no error

W is only ever consumed, never produced, so bivariance buys nothing here.

Changes

  • lib.dom.d.ts / lib.webworker.d.ts: declare write as a function property (write: (chunk?: W) => Promise<void>;), making WritableStream<W> contravariant in W under strictFunctionTypes. Both cases above now error; widening in the sound direction (WritableStream<{ a }> where WritableStream<{ a; b }> is expected) still compiles.
  • New compiler test writableStreamChunkVariance.ts plus baselines, committed pre-fix (no errors) and then updated, so the behavior change is visible in the diff.

These lib files are generated from TypeScript-DOM-lib-generator; the same change needs to be mirrored upstream to survive regeneration.

@typescript-automation typescript-automation Bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Sep 19, 2026
Copilot AI linked an issue Sep 19, 2026 that may be closed by this pull request
@typescript-automation typescript-automation Bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug and removed For Milestone Bug PRs that fix a bug with a specific milestone labels Sep 19, 2026
Copilot AI and others added 2 commits September 19, 2026 01:55
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot AI changed the title [WIP] Consider making WritableStreamDefaultWriter.write() contravariant Make WritableStreamDefaultWriter.write contravariant in its chunk type Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider making WritableStreamDefaultWriter.write() contravariant

2 participants