lib.dom.d.ts: Transformer is missing the `cancel` member that WHATWG Streams defines

オープン
#65,008 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

  • #65023 @timurzvural による — マージされずにクローズ

評価

難易度
3/5
見積もり時間
半日
初心者へのやさしさ
52/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
tooling

調査の方向性

Start by reading the generated DOM and WebWorker declarations for Transformer, especially dom.generated.d.ts and lib.webworker.d.ts, and check how their callback types are defined. The issue reports a closed, unmerged pull request, so first review that work and the current declaration generation flow. Done means both interfaces expose the standard cancel member with an appropriate callback type and the reported TypeScript reproduction compiles.

索引モデルが issue の本文から書いたものです。

説明

Summary

lib.dom.d.ts declares interface Transformer without the cancel member, but
the WHATWG Streams standard includes it and Node implements it. Any
new TransformStream({ ..., cancel() {} }) therefore fails to compile with
TS2353, even though the handler is valid and gets called when the readable side
is cancelled.

Reproduced on TypeScript 6.0.3 and 7.0.2.

Standards reference

WHATWG Streams, Transformer dictionary
(https://streams.spec.whatwg.org/#dictdef-transformer) defines six members:

start, transform, flush, cancel, readableType, writableType

lib.dom.d.ts declares five of the six. cancel is missing:

interface Transformer<I = any, O = any> {
    flush?: TransformerFlushCallback<O>;
    readableType?: undefined;
    start?: TransformerStartCallback<O>;
    transform?: TransformerTransformCallback<I, O>;
    writableType?: undefined;
}

Minimal reproduction

const ts = new TransformStream<Uint8Array, Uint8Array>({
  transform(chunk, controller) { controller.enqueue(chunk); },
  cancel() { /* client disconnected */ },
});
export { ts };
$ tsc --noEmit --target es2022 --lib dom,es2022 probe.ts
probe.ts(3,3): error TS2353: Object literal may only specify known properties,
and 'cancel' does not exist in type
'Transformer<Uint8Array<ArrayBufferLike>, Uint8Array<ArrayBufferLike>>'.

Identical output on 6.0.3 and 7.0.2.

The runtime does call it

Node 24.18.0, stream/web:

const ts = new TransformStream({
  transform(c, ctrl) { ctrl.enqueue(c); },
  cancel(reason) { console.log("transformer.cancel INVOKED", reason); },
});
await ts.readable.cancel("client-disconnect");
// -> transformer.cancel INVOKED client-disconnect

So cancel is not a dead optional member. It is the only hook that fires when
the readable side is cancelled, which for an SSE proxy is exactly the
client-disconnect case. Handlers here typically clear heartbeat and progress
intervals; removing them to satisfy the checker leaks a timer per abandoned
stream.

Why a declaration-merge workaround does not work

Filing this after trying the obvious local patch. Inside a module:

declare global {
  interface Transformer<I = unknown, O = unknown> {
    cancel?: (reason?: unknown) => void | PromiseLike<void>;
  }
}

This does not merge. Interface declarations must agree on their type
parameters, and lib.dom.d.ts uses <I = any, O = any> while the natural
patch uses unknown. With skipLibCheck: true the resulting TS2428 is
suppressed, so the merge silently fails and cancel stays absent. Matching
any makes it merge, but then the default type arguments on every
TransformStream in the codebase degrade to any, which trades one problem
for another.

That dead end is the reason this is filed as a lib gap rather than handled
locally.

Suggested fix

Add the member to Transformer in dom.generated.d.ts, with a callback type
next to the other three:

interface Transformer<I = any, O = any> {
    flush?: TransformerFlushCallback<O>;
    readableType?: undefined;
    start?: TransformerStartCallback<O>;
    transform?: TransformerTransformCallback<I, O>;
    cancel?: TransformerCancelCallback;
    writableType?: undefined;
}

type TransformerCancelCallback = (reason?: any) => void | PromiseLike<void>;

lib.webworker.d.ts has the same interface and needs the same change.

Notes

  • Checked 6.0.3 and 7.0.2; both reproduce.
  • Searched the issue tracker for Transformer cancel and lib.dom TransformStream
    before filing; the results were unrelated, so this does not look like a
    duplicate.
  • TypeScript version 7 ships lib files inside the platform binary, so the
    reproduction uses the CLI rather than reading lib.dom.d.ts from disk.
主要言語
Go
スター
112k
フォーク
15.4k
平均マージ
2日 21時間
マージ済み PR(30日)
126

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

microsoft/TypeScript のほかの issue

microsoft/TypeScript の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。