Generate Typescript types for Rust JSG Bindings #8077
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 591 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.382
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #8077"
general
weight: 0.35
nathanwhit
Motivation
@cloudflare/workers-typesis generated from the RTTI export (workerd:rtti), which only knows about C++ bindings. Rust modules register a callback the exporter can't inspect, and its overload for them is aTODOthat does nothing. As more APIs are written in Rust, they need to show up in the generated types like C++ ones do.Design
1. Members are added to a registry
To describe a resource, the RTTI export needs the list of members that the resource's JavaScript class actually gets.
Resource::membersnowadds each member to a registry instead of returning a list, like the
Registryparameter of C++registerMembers:Template creation and the RTTI export run the same
members, each with its own registry. Template creation keeps thecallbacks and never builds a type description; RTTI keeps the type descriptions and never builds a callback. So the
TypeScript declarations match what each resource registers under the export's compatibility flags, including choices like
those above.
2.
TypeInfodescribes Rust typesjsg::TypeInfodescribes how a Rust type looks in JavaScript, using the existingrtti.capnpschema and without an isolate.#[jsg_resource],#[jsg_struct]and#[jsg_oneof]generate the implementation; built-in and custom conversion types implement it by hand. Thetypes/generator is unchanged.Here are the type translations:
| Rust | TypeScript |
| --- | --- |
|
Option<T>|T \| undefined(optional) ||
jsg::Nullable<T>|T \| null(input also readsundefinedasnull; the parameter is required) ||
jsg::NullOrUndefined<T>,jsg::Lenient<T>|T \| null \| undefined(optional) ||
Vec<T>,&[T]|T[](numbers included) ||
jsg::Dict<V>|Record<string, V>||
v8::Local<v8::Uint8Array>and other typed arrays |Uint8Array, etc. ||
Result<T, E>|T||
SystemTime|Date(arguments also acceptnumber) ||
v8::Local<v8::Date>|Date||
i64,u64,v8::Local<BigInt>|number \| bigint(as C++ describesint64_t) ||
v8::Local<Promise>|Promise<any>(arguments also accept plain values, as in C++) |3. Module types are included in the export
To describe a module, the exporter needs its exports type without running the module. So module callbacks now return the exports resource, and registration converts it to JS:
Callbacks still capture nothing, and an error a callback returns is still thrown to the importer.
Registration passes the C++ registry a second function pointer next to the callback, which describes the exports type.
RustModuleRegistryuses anif constexprto check whether the wrapped registry hasaddRustBuiltinModule; only the exporter does.Commits
1. Register Rust resource members through a
MemberRegistry. Runtime only; contains the API change.2. Describe Rust JSG bindings through
TypeInfo.3. Include Rust module types in the RTTI export.
Not in this PR
JSG_TS_OVERRIDE/JSG_TS_DEFINEequivalents: a follow-up PR.