Skip to content

Add nice type generation for ctx.exports and ctx.props.#5004

Merged
kentonv merged 9 commits intomainfrom
kenton/ctx-exports-types
Sep 11, 2025
Merged

Add nice type generation for ctx.exports and ctx.props.#5004
kentonv merged 9 commits intomainfrom
kenton/ctx-exports-types

Conversation

@kentonv
Copy link
Copy Markdown
Member

@kentonv kentonv commented Sep 7, 2025

This makes ctx.exports have the right types automatically, as long as the project has a type override like:

declare namespace Cloudflare {
  interface GlobalProps {
    // Reflects back the main module's type.
    mainModule: typeof import("./src");

    // Lists DO namespaces that are configured with storage (via `new_sqlite_classes`).
    durableNamespaces: "MyDurableObject" | "AnotherDurableObject";
  }
}

Also:

  • I changed the default type for Env to be Cloudflare.Env instead of unknown in several places.
  • I have added a type parameter to define ctx.props to WorkerEntrypoint and DurableObject. Unfortunately it has to be the second parameter, after Env, which is annoying since it'd be nice to leave Env as the default. Maybe if we rename these to StatelessWorker/StatefulWorker we can change the parameter order at that time to make props first?

DO NOT MERGE this until ctx.exports is verified as working in prod: I unmarked it "experimental" because otherwise it was going to be annoying to maintain separate overrides. We are very close on this so I think it'll be OK. I added back the experimental guard so that I can land this.

@kentonv kentonv requested a review from penalosa September 7, 2025 17:46
@kentonv kentonv requested review from a team as code owners September 7, 2025 17:46
@kentonv kentonv force-pushed the kenton/ctx-exports-types branch from 7bbe709 to ab815e7 Compare September 7, 2025 18:29
@kentonv kentonv force-pushed the kenton/ctx-exports-types branch from d142c62 to 8848e56 Compare September 10, 2025 14:40
@kentonv
Copy link
Copy Markdown
Member Author

kentonv commented Sep 10, 2025

(rebased)

@kentonv kentonv changed the title Add nice type generation for ctx.exports and ctx.props. Also un-flag it. Add nice type generation for ctx.exports and ctx.props. Sep 10, 2025
@kentonv
Copy link
Copy Markdown
Member Author

kentonv commented Sep 10, 2025

I've updated this so WorkerLoader is included in the types, and I also put ctx.exports back behind the experimental flag for now so that I can safely land this before it's ready in prod.

@kentonv kentonv force-pushed the kenton/ctx-exports-types branch from 37d18f9 to 251750b Compare September 10, 2025 23:02
@kentonv kentonv force-pushed the kenton/ctx-exports-types branch from 251750b to cd74ead Compare September 11, 2025 13:41
@kentonv kentonv enabled auto-merge September 11, 2025 13:42
On all of:
* ExecutionContext
* DurableObjectState
* WorkerEntrypoint
* DurbaleObject
I noticed this list was out-of-date compared to rtti.c++.

Note I couldn't add modules.h nor pyodide.h as they don't seem to be declare dependencies. Unclear if that's important.

This doesn't seem to have had any effect on the generated types, so I'm not sure whether it matters, but the comment seemed to suggest this list should be kept in sync with rtti.c++.
This also works around issue #4882 in the type generation by overriding `$class` to `class` manually.
Unfortunately I cannot figure out how to run this locally, so I'm testing in CI.
I think there's no real reason to let you override this?
Decided I need to do this in order to get the ball rolling, but this commit can easily be reverted later when we're ready.
@kentonv kentonv force-pushed the kenton/ctx-exports-types branch from cd74ead to eeae70a Compare September 11, 2025 15:02
@kentonv kentonv merged commit 48ce622 into main Sep 11, 2025
21 of 22 checks passed
@kentonv kentonv deleted the kenton/ctx-exports-types branch September 11, 2025 15:41
@shmuli9
Copy link
Copy Markdown

shmuli9 commented Nov 27, 2025

This breaks my typechecking - is there a way to opt out of generating the GlobalProps interface?
#11545

@kentonv
Copy link
Copy Markdown
Member Author

kentonv commented Dec 1, 2025

@shmuli9 This PR does not implement the GlobalProps type generation, it only uses it. The generation happens in wrangler. Your bug is filed in the right place, but this PR didn't cause it.

@shmuli9
Copy link
Copy Markdown

shmuli9 commented Dec 1, 2025

Ah, thanks for that @kentonv. I was under the impression this PR implemented it, so thanks for the info
Would you be able to point me at the right PR?

Also, thanks for contributing to this repo 💪 - didn't mean to be personal in my bug report. I just thought this was the place for it. Definitely appreciate the active development here 👨‍💻

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.

6 participants