Implement types-as-a-service and check bundles with generated types #1959
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hey! 👋 This PR implements a Worker for generating TypeScript types given a compatibility date and set of compatibility flags. The aim here is to remove the confusing entrypoints system from
@cloudflare/workers-types
, and replace this with...compatibility_date
andcompatibility_flags
in yourwrangler.toml
, using the user's locally installed version ofworkerd
.Our previous type generation didn't support generating types for built-in modules. Given lots of features are now within modules (
cloudflare:workers
,nodejs_compat
, etc), this doesn't quite cut the mustard anymore. This new system supports generating types for C++ and TypeScript modules! 🎉 This means.../src/node
and/src/cloudflare
with auto-generated types, as opposed to handwritten definitions and@types/node
@types/node
when usingnodejs_compat
, and will only get types for Node APIs we supportType checking our own code with types generated from our code risks introducing circular build dependencies. To get around this, this PR splits up registering C++ modules (
index.h
) and registering modules from bundles (index-bundles-rtti.h
).api-encoder.c++
only includesindex.h
, whereas theworkerd:rtti
module includes both. A new//types:types_internal
entrypoint has been added that uses the output fromapi-encoder.c++
to generate a version of the types containing definitions from C++ concatenated with/types/defines/*.d.ts
. Bundles are type checked with this, then included in a new//src/workerd/api:index_bundles_rtti
target which is referenced by//src/workerd/server:server
.The PR doesn't change the format of the
@cloudflare/workers-types
package for now—we still generate multiple entrypoints for each compatibility date that changes the public API surface. We'll probably want to deprecate this package at some point, in favour of the new yet-to-be-written tool for generating types locally.Potential Improvements
workerd
version and compatibility flags@types/node
like we do withlib.webworker.d.ts
opt
builds/tests (the Worker is ~10MB uncompressed and unminified, which is pretty slow to load/run in debug builds)