workerd icon indicating copy to clipboard operation
workerd copied to clipboard

The `request` type inferred from `ExportedHandler` is `any` instead of `Request`.

Open tavoyne opened this issue 1 year ago • 7 comments

image

tavoyne avatar Aug 03 '24 16:08 tavoyne

Can you provide a reproduction (perhaps git repository) that we can use to diagnose this problem?

petebacondarwin avatar Aug 05 '24 13:08 petebacondarwin

I am having the same issue right now. For me @cloudflare/[email protected] + [email protected] were definitely working, and I see this issue appearing with the at latest @cloudflare/[email protected] + [email protected], although possible earlier.

asimpletune avatar Aug 19 '24 12:08 asimpletune

I did sort of a bisect of wrangler versions x @cloudflare/worker-types. The last version of wrangler that I found compatible with the last working version of worker-types was 3.36.0 x 4.20240208.0.

In other words, npm install [email protected] @cloudflare/[email protected] --save-dev is the latest version of the two working together, for me at least.

One caveat though is that I was testing this against my particular use case, which was with PagesFunction<Env>, not ExportedHandler.

asimpletune avatar Aug 19 '24 13:08 asimpletune

I could be wrong but it looks like the issue was introduced about 6 months ago, here https://github.com/cloudflare/workerd/blame/a47804f8bbcd06904bb9eae91e0e6c3ffdc7f6d6/types/defines/pages.d.ts#L4.

It looks like Request doesn't have generics, although they're being used here.

asimpletune avatar Aug 19 '24 13:08 asimpletune

The Request defined in @cloudflare/workers-types does have generics, so that should work, I think.

@asimpletune Would you be able to provide a reproduction repo that demonstrates the issue?

penalosa avatar Aug 21 '24 10:08 penalosa

Hi! Id love to but I can't for a few weeks. But if you create a new project with the versions I listed above you will see the same results.

Although, the version I cited is when the issue was first introduced, so it should be present with wrangler 3.72.0 and its sibling worker types file.

Otherwise I can create a new issue when I'm available again, since the issue I brought up is only similar to the OP but not guaranteed to be the same.

asimpletune avatar Aug 21 '24 18:08 asimpletune

I have encountered the same question image https://github.com/yunsii/cf-worker-status-page-pro/blob/7896301356277d72d9fc1ced420ad495547f3318/src/worker/index.ts#L24-L32

yunsii avatar Sep 07 '24 10:09 yunsii

There's a couple of issues with your setup @yunsii:

  • Firstly, you're hitting https://github.com/cloudflare/workerd/issues/1298 and will need to pin your @types/node to an older version to prevent Request being overriden here;
  "pnpm": {
    "overrides": {
      "@types/node": "20.8.3"
    }
  },
  • Then you're also loading DOM types in your lib config in your tsconfig, which also has its own version of Request. Remove this.

After this, the request type works fine in your example.

If you have a need for DOM types, I'd recommend a different tsconfig.json for your worker, and your frontend.


For others experiencing this issue, I'd double check those two things - your lib in your tsconfig.json, and that @types/node is pinned to an old version, as per the previously linked issue.

Cherry avatar Sep 08 '24 13:09 Cherry

@Cherry Thanks for your detailed explanation, it is a interesting issue 😄

yunsii avatar Sep 08 '24 13:09 yunsii

hi all :) for anyone still running into this, please check out @Cherry 's followup comment above. if you are still having problems, please feel free to open a new issue referencing this one with a repro and any additional details and we will revisit and investigate. thanks!

lrapoport-cf avatar Sep 23 '24 13:09 lrapoport-cf