← Back to Pull requests

Generate Typescript types for Rust JSG Bindings #8077

Closed
unknown closed this pull request without merging on Nov 03, 2026 03:39 UTC
View on GitHub
unknown unknown commented on Oct 28, 2026 21:53 UTC
Author

Motivation

@cloudflare/workers-types is 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 a TODO that 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::members now
adds each member to a registry instead of returning a list, like the Registry parameter of C++ registerMembers:

   // before
   fn members(flags: compatibility_flags::Reader<'_>) -> Vec<Member>
   // after
   fn members(registry: &mut impl MemberRegistry, flags: compatibility_flags::Reader<'_>)
   

Template creation and the RTTI export run the same members, each with its own registry. Template creation keeps the
callbacks 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. TypeInfo describes Rust types

jsg::TypeInfo describes how a Rust type looks in JavaScript, using the existing rtti.capnp schema and without an isolate. #[jsg_resource], #[jsg_struct] and #[jsg_oneof] generate the implementation; built-in and custom conversion types implement it by hand. The types/ generator is unchanged.

#[jsg_struct(name = "LookupResult")]
struct ResultData {
    pub label: String,
    pub detail: jsg::Nullable<String>,
}

#[jsg_resource(name = "Example", ts_root)]
struct Example;

#[jsg_resource]
impl Example {
    #[jsg_method(name = "lookup")]
    fn lookup_value(&self, label: String, detail: jsg::Nullable<String>) -> jsg::Result<ResultData> { /* ... */ }

    #[jsg_method]
    fn hint(&self, hint: Option<jsg::Nullable<String>>) -> Option<jsg::Nullable<String>> { /* ... */ }

    #[jsg_property(prototype, name = "title", readonly)]
    fn get_label(&self) -> String { /* ... */ }
}

interface LookupResult {
  label: string;
  detail: string | null;
}
declare class Example {
  lookup(label: string, detail: string | null): LookupResult;
  hint(hint?: string | null): string | null | undefined;
  get title(): string;
}

Here are the type translations:

| Rust | TypeScript |
| --- | --- |
| Option<T> | T \| undefined (optional) |
| jsg::Nullable<T> | T \| null (input also reads undefined as null; 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 accept number) |
| v8::Local<v8::Date> | Date |
| i64, u64, v8::Local<BigInt> | number \| bigint (as C++ describes int64_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:

// before
jsg::modules::add_builtin(registry, "node-internal:dns", |lock| DnsUtil::new().to_js(lock), ModuleType::Internal);
// after
jsg::modules::add_builtin(registry, "node-internal:dns", |_lock| Ok(DnsUtil::new()), ModuleType::Internal);

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. RustModuleRegistry uses an if constexpr to check whether the wrapped registry has addRustBuiltinModule; only the exporter does.

  • The exporter calls the describe pointer and never runs the module. It describes every Rust module into one shared context under the export's compatibility flags, then adds the result to the C++ types and modules.

  • Runtime registries ignore the describe pointer.
  • 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_DEFINE equivalents: a follow-up PR.
  • Diff: 591 lines (+443 / -148) across 7 files Reviewer: nathanwhit Review duration: 7545m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.0% (MEDIUM) · Higher = larger blast radius
    50.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%