Skip to content

fix(cloudflare): Restore env type inference for options callbacks - #22963

Open
JPeer264 wants to merge 2 commits into
developfrom
jp/cloudflare-env-type-inference
Open

fix(cloudflare): Restore env type inference for options callbacks#22963
JPeer264 wants to merge 2 commits into
developfrom
jp/cloudflare-env-type-inference

Conversation

@JPeer264

@JPeer264 JPeer264 commented Aug 3, 2026

Copy link
Copy Markdown
Member

closes #18294
closes #18294

Our types were pretty messed up and we didn't have any tests if we break something. Since there is now workers-types v4 AND v5 we need to have a kinda matrix. To keep it simple the matrix is just 2 different ts configs: tsconfig.v4.json and tsconfig.v5.json

We need to support different ways of writing while keeping the env types in tact. Those are some samples:

// types inferred directly from the options
Sentry.withSentry((env: Env) => ({}), myHandler)

// types given as generic (we can drop that if necessary)
Sentry.withSentry<Env>(env => ({}), myHandler)

// types inferred from ExportedHandler
Sentry.withSentry(env => ({}), myHandler as ExportedHandler<Env>)

// types inferred from WorkerEntrypoint
class MyEntrypoint extends WorkerEntrypoint<Env> {}
Sentry.withSentry(env => ({}), MyEntrypoint)

// types from the global Cloudflare.Env (which is recommended by Cloudflare themselves)
class MyEntrypoint extends WorkerEntrypoint {}
Sentry.withSentry(env => ({}), MyEntrypoint)

// TanStack start. The `tanstackstart-react-clooudflare` E2E test ignores the types there
// so it is better to remove the `@ts-expect-error`
// also this needs to support the same variations as written above
export default Sentry.withSentry(env => ({}), wrapFetchWithSentry(handler));

// same for hono

It seems by just setting Env in the generics to any has done the trick, but needs some more type helpers in order to get the correct Env in the options, based on the different ways of writing it.


Clanker generated output:

The env parameter of the options callbacks of withSentry, instrumentDurableObjectWithSentry and instrumentWorkflowWithSentry collapsed to unknown when the handler was typed via satisfies, a pre-typed constant, a Hono app or a bare WorkerEntrypoint / DurableObject / WorkflowEntrypoint class, so property access on env failed to compile.

Resolve the callback env from the passed handler first (its handler methods or class constructor), then the explicit Env generic, then the wrangler-generated Cloudflare.Env, and fall back to any so untyped setups keep compiling. Also loosen the constructor constraint of instrumentWorkflowWithSentry: workers-types defaults WorkflowEntrypoint's Env to unknown (unlike WorkerEntrypoint / DurableObject), so bare workflow classes were rejected in wrangler-types projects.

Add a tsc type-test suite to the Cloudflare integration tests covering the inference scenarios against workers-types v4 and the built d.ts, including a separate program simulating a wrangler types project, and drop the now-unneeded @ts-expect-error in the TanStack Start e2e app.

The env parameter of the options callbacks of withSentry,
instrumentDurableObjectWithSentry and instrumentWorkflowWithSentry
collapsed to unknown when the handler was typed via satisfies, a
pre-typed constant, a Hono app or a bare WorkerEntrypoint /
DurableObject / WorkflowEntrypoint class, so property access on env
failed to compile.

Resolve the callback env from the passed handler first (its handler
methods or class constructor), then the explicit Env generic, then the
wrangler-generated Cloudflare.Env, and fall back to any so untyped
setups keep compiling. Also loosen the constructor constraint of
instrumentWorkflowWithSentry: workers-types defaults
WorkflowEntrypoint's Env to unknown (unlike WorkerEntrypoint /
DurableObject), so bare workflow classes were rejected in
wrangler-types projects.

Add a tsc type-test suite to the Cloudflare integration tests covering
the inference scenarios against workers-types v4 and the built d.ts,
including a separate program simulating a wrangler types project, and
drop the now-unneeded @ts-expect-error in the TanStack Start e2e app.

Co-Authored-By: Claude <noreply@anthropic.com>
@JPeer264 JPeer264 self-assigned this Aug 3, 2026
@JPeer264
JPeer264 requested a review from a team as a code owner August 3, 2026 14:38
@JPeer264
JPeer264 requested review from andreiborza and mydea and removed request for a team August 3, 2026 14:38
@linear-code

linear-code Bot commented Aug 3, 2026

Copy link
Copy Markdown

JS-1202

@JPeer264
JPeer264 requested review from nicohrubec and removed request for mydea August 3, 2026 14:43
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 29.91 kB - -
@sentry/browser - with treeshaking flags 28.12 kB - -
@sentry/browser (incl. Tracing) 47.36 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.36 kB - -
@sentry/browser (incl. Tracing, Profiling) 52.13 kB - -
@sentry/browser (incl. Tracing, Replay) 86.7 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 76.13 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 91.41 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 104.05 kB - -
@sentry/browser (incl. Feedback) 47.22 kB - -
@sentry/browser (incl. sendFeedback) 34.76 kB - -
@sentry/browser (incl. FeedbackAsync) 39.87 kB - -
@sentry/browser (incl. Metrics) 30.99 kB - -
@sentry/browser (incl. Logs) 31.22 kB - -
@sentry/browser (incl. Metrics & Logs) 31.9 kB - -
@sentry/react 31.73 kB - -
@sentry/react (incl. Tracing) 49.61 kB - -
@sentry/vue 34.98 kB - -
@sentry/vue (incl. Tracing) 49.34 kB - -
@sentry/svelte 29.94 kB - -
CDN Bundle 32.01 kB - -
CDN Bundle (incl. Tracing) 47.72 kB - -
CDN Bundle (incl. Logs, Metrics) 33.55 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.08 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 72.9 kB - -
CDN Bundle (incl. Tracing, Replay) 85.35 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.65 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.14 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.46 kB - -
CDN Bundle - uncompressed 95.52 kB - -
CDN Bundle (incl. Tracing) - uncompressed 143.1 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 100.23 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 147.08 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 224.99 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 262.36 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 266.33 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 276.07 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 280.02 kB - -
@sentry/nextjs (client) 52.21 kB - -
@sentry/sveltekit (client) 47.81 kB - -
@sentry/core/server 80.01 kB - -
@sentry/core/browser 51.97 kB - -
@sentry/node 120.89 kB - -
@sentry/node/import (ESM hook with diagnostics-channel injection) 0 B added added
@sentry/node - without tracing 84.26 kB +0.01% +1 B 🔺
@sentry/aws-serverless 92.94 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 219.37 kB +0.01% +4 B 🔺
@sentry/cloudflare (withSentry) 540.57 kB +0.02% +98 B 🔺

View base workflow run

@JPeer264
JPeer264 requested a review from a team as a code owner August 3, 2026 16:06
@JPeer264
JPeer264 requested review from s1gr1d and removed request for a team August 3, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cloudflare types for env no longer work

1 participant