Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jldec opened this issue Jul 5, 2024 · 2 comments · Fixed by #6333
Closed

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

jldec opened this issue Jul 5, 2024 · 2 comments · Fixed by #6333
Labels
bug Something that isn't working

Comments

@jldec
Copy link

jldec commented Jul 5, 2024

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>;
  1. Notice the TypeScript error
  2. Use VS Code to Goto the type definition of fetch(). This leads to node_modules/.pnpm/@[email protected]/node_modules/@types/node/globals.d.ts
  3. The error disappears if you rename the file vitest.config.mts to vitest.config.mts_
  4. After renaming that file, the type definition for fetch() leads to node_modules/.pnpm/@[email protected]/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

@jldec jldec added the bug Something that isn't working label Jul 5, 2024
@jldec jldec changed the title Wrong fetch types result in Typescript errors for ReadableStream in workers generated by npm create cloudflare@latest TypeScript errors for ReadableStream in workers generated by npm create cloudflare@latest Jul 6, 2024
@jldec jldec changed the title TypeScript errors for ReadableStream in workers generated by npm create cloudflare@latest TypeScript errors for ReadableStream in C3 "Hello World" worker Jul 6, 2024
@Cherry
Copy link
Contributor

Cherry commented Jul 7, 2024

cloudflare/workerd#1298 is also related here.

@jldec jldec changed the title TypeScript errors for ReadableStream in C3 "Hello World" worker TypeScript errors for ReadableStream in npm create cloudflare "Hello World" worker Jul 9, 2024
@jldec
Copy link
Author

jldec commented Jul 13, 2024

For reference
microsoft/TypeScript#37053
and
cloudflare/workerd#1775

@penalosa penalosa linked a pull request Jul 25, 2024 that will close this issue
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants