Hi, you guys might want to extend the cf docs on worker RPC to include the utility type `InstanceTyp

Hi, you guys might want to extend the cf docs on worker RPC to include the utility type InstanceType. It automatically types your RPC classes so you could use your RPC methods in other workers with type-safety.

https://developers.cloudflare.com/workers/runtime-apis/rpc/

An example:

import { RpcTarget, WorkerEntrypoint } from 'cloudflare:workers';

class Counter extends RpcTarget {
    #value = 0;

    increment(amount: number = 1) {
        this.#value += amount;
        return this.#value;
    }

    get value() {
        return this.#value;
    }
}

export class CounterService extends WorkerEntrypoint {
    async newCounter() {
        return new Counter();
    }
}

export type CounterServiceStub = InstanceType<typeof CounterService>;


Then, use the CounterServiceStub in your RPC client for type-checked method signatures for the downstream worker you are calling.
Cloudflare Docs
The built-in, JavaScript-native RPC system built into Workers and Durable Objects.
Was this page helpful?