This allows undefined to be enqueued into the stream while the reader of the stream still expects to read R out of it.
This is demonstrated in the following example:
const stream = new ReadableStream<string>({
start: (controller) => {
// currently the signature is
// ReadableStreamDefaultController<string>.enqueue(chunk?: string | undefined): void
// undefined is enqueued here
controller.enqueue();
},
});
// ReadableStreamDefaultReader<string>
const reader = stream.getReader();
// ReadableStreamReadResult<string>
const result = await reader.read();
if (result.done === false) {
// result is narrowed down to ReadableStreamReadValueResult<string>
// TypeScript assumes ReadableStreamReadValueResult<string>.value is string
// so the compilation will succeed
// but result.value is actually undefined
result.value.toUpperCase();
}
The Web IDL definition specifies an
enqueue
method for the ReadableStreamDefaultController class with the following signature:The
chunk
parameter is optional which probably is fine since chunk isany
.However, this is a little problematic with TypeScript generics. Currently, the generated TypeScript interface looks like this:
This allows
undefined
to be enqueued into the stream while the reader of the stream still expects to readR
out of it.This is demonstrated in the following example:
To address this, could we consider overriding the
chunk
parameter to be required? There are similar issues with TransformStreamDefaultController.enqueue and WritableStreamDefaultWriter.write.