cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.61k stars 680 forks source link

TypeScript errors for `ReadableStream` in npm create cloudflare "Hello World" worker #6211

Closed jldec closed 2 months ago

jldec commented 3 months ago

Which Cloudflare product(s) does this pertain to?

Impacts C3 "Hello World" TypeScript users - suspected bug in Workers Vitest Integration

What version(s) of the tool(s) are you using?

@cloudflare/vitest-pool-workers: ^0.4.5

What version of Node are you using?

v20.11.1

What operating system and version are you using?

Sonoma 14.5

Describe the Bug

Observed behavior

Typescript errors if you use the response.body of a fetch in a worker generated by npm create cloudlare@latest.

Argument of type 'ReadableStream<any> | null' is not assignable to parameter of type 'BodyInit | null | undefined'.
  Type 'ReadableStream<any>' is not assignable to type 'BodyInit | null | undefined'.
    Type 'ReadableStream<any>' is not assignable to type 'ReadableStream<Uint8Array>'.
      The types returned by 'getReader(...)' are incompatible between these types.
        Property 'readAtLeast' is missing in type 'ReadableStreamDefaultReader<any>' but required in type 'ReadableStreamBYOBReader'.

The root cause appears to be this import which replaces the @cloudflare/workers-types for fetch() with @types/node types. import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'

Credit should go to @pronoun who figured this out and posted in discord https://discord.com/channels/595317990191398933/1251292244766621787/1251292244766621787

Expected behavior

vitest-pool-workers should not override fetch.

Steps to reproduce

  1. npm create cloudflare@latest with default "Hello World" worker with typescript
  2. modify the code to call fetch() and return the response.body in a new Response (as in this example)
    export default {
    async fetch(request, env, ctx): Promise<Response> {
        const response = await fetch("https://time.jldec.workers.dev/");
        return new Response(response.body, response);
    },
    } satisfies ExportedHandler<Env>;
  3. Notice the TypeScript error
  4. Use VS Code to Goto the type definition of fetch(). This leads to node_modules/.pnpm/@types+node@20.14.9/node_modules/@types/node/globals.d.ts
  5. The error disappears if you rename the file vitest.config.mts to vitest.config.mts_
  6. After renaming that file, the type definition for fetch() leads to node_modules/.pnpm/@cloudflare+workers-types@4.20240620.0/node_modules/@cloudflare/workers-types/2023-07-01/index.d.ts

Screenshots showing different fetch types.

Screenshot 2024-07-05 at 19 08 36

Screenshot 2024-07-05 at 19 09 08

Please provide a link to a minimal reproduction

https://github.com/jldec/cloudflare-worker-fetch-type-issue

Please provide any relevant error logs

No response

Cherry commented 3 months ago

https://github.com/cloudflare/workerd/issues/1298 is also related here.

jldec commented 2 months ago

For reference https://github.com/microsoft/TypeScript/issues/37053 and https://github.com/cloudflare/workerd/issues/1775