workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

fix: wrangler types decouple env-interface from namespace

Open HW13 opened this issue 1 month ago • 5 comments

Fixes #10020

Describe your change...

Wrangler types command now adds a BaseEnv interface which is then extended by Cloudflare.Env and the --env-interface. Example output:

/* eslint-disable */
// Generated by Wrangler by running \`wrangler types --env-interface MyCloudflareEnv --include-runtime=false\` (hash: ...)
interface BaseEnv {
	MY_VAR: "my-var-value";
}
declare namespace Cloudflare {
	interface GlobalProps {
		mainModule: typeof import("./src/index");
	}
	interface Env extends BaseEnv {}
}
interface MyCloudflareEnv extends BaseEnv {}

While this doesn't outright resolve #10020, it ensures that the env-interface for each worker no longer depends on the namespace. This allows it to be explicitly passed into the WorkerEntrypoint or type cast to the env - which will prevent issues with conflicting Cloudflare namespaces.

import { WorkerEntrypoint } from 'cloudflare:workers'

export class MyService extends WorkerEntrypoint<MyCloudflareEnv> {
  // ...
}
import { env } from 'cloudflare:workers'

export const myCloudflareEnv = env as MyCloudflareEnv


  • Tests
    • [x] Tests included/updated
    • [ ] Tests not necessary because:
  • Public documentation
    • [ ] Cloudflare docs PR(s):
    • [x] Documentation not necessary because: References to the worker-configuration.d.ts in the docs appear to be intentionally simplified and don't include the Cloudflare namespace (example)
    • [ ] Wrangler PR:
    • [x] Not necessary because: Not sure about this one, happy to do it if needed though

HW13 avatar Nov 29 '25 22:11 HW13

🦋 Changeset detected

Latest commit: ab98ff6f461d8dc4ad3c64975b7da91038a868dd

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Nov 29 '25 22:11 changeset-bot[bot]

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@11471
@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@11471
miniflare

npm i https://pkg.pr.new/miniflare@11471
@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@11471
@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@11471
@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@11471
@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@11471
@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@11471
@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@11471
wrangler

npm i https://pkg.pr.new/wrangler@11471

commit: 3d44a76

pkg-pr-new[bot] avatar Dec 02 '25 09:12 pkg-pr-new[bot]

Tests have been updated and the latest changes from main have been rebased into this branch. Could I please get workflow approval? 🙏

HW13 avatar Dec 06 '25 22:12 HW13

Ok, so it looks like I'll need to do a v3 backport PR. Will get that up asap 👍

HW13 avatar Dec 11 '25 21:12 HW13

@HW13 - thanks for the work here. We were just looking at this yesterday. Although this appears to fix some aspects of the problem, it will also fall over for other ambient types, which could be different in each worker based on compat flags etc. I think that the long term solution is to find a way to generate an "external" type for a Worker that can be consumed safely in a service binding. But need to think about how that could be done. In the meantime, we'll discuss whether this PR is OK to land for now and get back to you early next week.

petebacondarwin avatar Dec 12 '25 08:12 petebacondarwin