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'
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
The error disappears if you rename the file vitest.config.mts to vitest.config.mts_
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
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
.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/1251292244766621787Expected behavior
vitest-pool-workers should not override fetch.
Steps to reproduce
npm create cloudflare@latest
with default "Hello World" worker with typescriptfetch()
. This leads tonode_modules/.pnpm/@types+node@20.14.9/node_modules/@types/node/globals.d.ts
vitest.config.mts
tovitest.config.mts_
fetch()
leads tonode_modules/.pnpm/@cloudflare+workers-types@4.20240620.0/node_modules/@cloudflare/workers-types/2023-07-01/index.d.ts
Screenshots showing different fetch types.
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