Workers SDK Issue Reports

← Back to Dashboard

#8062 `wrangler types --x-include-runtime` generates wrong types for `ReadableStream`

Download Reproduction
Recommendation:KEEP OPEN
Difficulty:easy
Reasoning:

Bug reproduced with wrangler 4.60.0. `ReadableStream.from()` static method is available at runtime (since 2024-04-04) but not in generated types.

Suggested Action:

Keep open. Fix needed in workerd types to add `from` static method.

Analysis Report

Issue Review: cloudflare/workers-sdk#8062

Summary

wrangler types does not generate the ReadableStream.from() static method in runtime types, causing TypeScript errors.

Findings

  • Created: 2025-02-08
  • Updated: 2025-10-30
  • Version: 3.107.3 → 4.60.0 (wrangler)
  • Component: Wrangler types / workerd types
  • Labels: bug
  • Comments: 0

Key Evidence

  • Issue is reproducible with latest wrangler (4.60.0):

    • Created minimal reproduction with compatibility_date = "2024-04-04" (when ReadableStream.from() was introduced)
    • Running wrangler types generates types that do NOT include ReadableStream.from()
    • TypeScript fails with: Property 'from' does not exist on type '{ new (...): ReadableStream<...>; ... }'
  • Generated types inspection (worker-configuration.d.ts lines 2161-2165):

    declare const ReadableStream: {
        prototype: ReadableStream;
        new (underlyingSource: UnderlyingByteSource, strategy?: QueuingStrategy<Uint8Array>): ReadableStream<Uint8Array>;
        new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
    };
    
    • Missing: static from<R>(asyncIterable: AsyncIterable<R> | Iterable<R>): ReadableStream<R>
  • No PRs found that explicitly fix this issue or add ReadableStream.from types

  • Root cause likely in workerd types: Runtime types are generated from workerd, so the fix would need to happen in the workerd types definitions that wrangler consumes

Recommendation

Status: KEEP OPEN

Reasoning: The bug is confirmed to still exist in the latest version (wrangler 4.60.0). ReadableStream.from() is a valid runtime API available since compatibility date 2024-04-04, but the generated TypeScript types do not include this static method, causing false-positive type errors.

Action: Keep open. This requires a fix in the workerd types to add the from static method to the ReadableStream interface declaration.

Suggested Comment

Confirmed this is still an issue with wrangler 4.60.0. Running wrangler types with compatibility_date = "2024-04-04" generates types that do not include the ReadableStream.from() static method.

The generated ReadableStream declaration (in worker-configuration.d.ts) only includes constructor signatures, but not the from static method:

declare const ReadableStream: {
    prototype: ReadableStream;
    new (underlyingSource: UnderlyingByteSource, ...): ReadableStream<Uint8Array>;
    new <R = any>(underlyingSource?: UnderlyingSource<R>, ...): ReadableStream<R>;
};

The fix likely needs to happen in the workerd types that wrangler consumes for runtime type generation.


Solution Recommendation

Root Cause Analysis

The issue is in the workerd repository, not workers-sdk. The JSG_TS_OVERRIDE macro for ReadableStream completely replaces the generated type definition.

File: cloudflare/workerd/src/workerd/api/streams/readable.h:399-403

JSG_TS_OVERRIDE(const ReadableStream: {
  prototype: ReadableStream;
  new (underlyingSource: UnderlyingByteSource, strategy?: QueuingStrategy<Uint8Array>): ReadableStream<Uint8Array>;
  new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
});

The from() static method IS defined at line 353:

JSG_STATIC_METHOD(from);

However, when JSG_TS_OVERRIDE starts with const, type, class, etc., it replaces the entire definition rather than merging with generated members (per workerd/types/src/transforms/overrides/compiler.ts:13-27).

Proposed Solution

Modify the JSG_TS_OVERRIDE in workerd to include the from() method:

JSG_TS_OVERRIDE(const ReadableStream: {
  prototype: ReadableStream;
  new (underlyingSource: UnderlyingByteSource, strategy?: QueuingStrategy<Uint8Array>): ReadableStream<Uint8Array>;
  new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
  from<R>(asyncIterable: AsyncIterable<R> | Iterable<R>): ReadableStream<R>;  // ADD THIS
});

Implementation Difficulty: Easy

Factor Assessment
Lines of code Single line addition
Risk Low - isolated type change
Testing Easy - verify types include from()
Scope Single file in workerd repo
Breaking changes None

Note: This fix is in the cloudflare/workerd repository, not workers-sdk. Once fixed in workerd and a new version is released, workers-sdk will automatically pick up the fix when the dependency is updated.

Files to Modify (in cloudflare/workerd)

  1. src/workerd/api/streams/readable.h - Add from() to JSG_TS_OVERRIDE
  2. types/test/types/streams.ts - Add test for ReadableStream.from() type

Testing Recommendations

  1. Verify ReadableStream.from() appears in generated types with compat date 2024-04-04
  2. Type correctness test:
    const stream = ReadableStream.from([1, 2, 3]);
    const asyncStream = ReadableStream.from(async function* () { yield 1; }());
    
  3. E2E test in workers-sdk after workerd update

Notes & Feedback (1)

Add Note