workerd
workerd copied to clipboard
Durable Object RPC Type Errors
I ran into an issue with typescript in Durable Objects x RPC following these docs - https://developers.cloudflare.com/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#call-rpc-methods
Posted to discord a few days ago and sounds like I'm not the only one with this problem.
In the worker where I'm calling the RPC, I'm defining the env:
export interface Env {
// other vars
MERCHANT_INSTANCE: DurableObjectNamespace<MerchantInstance>
}
typescript throws this error:
TS2344: Type MerchantInstance does not satisfy the constraint DurableObjectBranded
Property [__DURABLE_OBJECT_BRAND] is missing in type MerchantInstance but required in type DurableObjectBranded
I'm importing the types in the worker and using most recent versions of all packages in both the DO and separate worker where I'm calling the DO:
{
"devDependencies": {
"@cloudflare/workers-types": "^4.20240405.0",
"typescript": "^5.0.4",
"wrangler": "^3.41.0"
}
}
DO class is defined as:
import { DurableObject } from "cloudflare:workers";
export interface Env {
}
export class MerchantInstance extends DurableObject {
constructor(state: DurableObjectState, env: Env) {
super(state, env)
}
async increment(amount = 1) {
let value: number = (await this.ctx.storage.get("value")) || 0;
value += amount;
await this.ctx.storage.put("value", value);
return value;
}
}
in addition to the typescript error in the Env type definition, I'm also not getting type completion on the 'increment method'. When calling the .increment() method on a stub, I get the TS error:
TS2339: Property increment does not exist on type DurableObjectStub<MerchantInstance>
issue is resolved by ensuring @cloudflare/workers-types/2024-04-05 is used in tsconfig and removing explicit type imports from the file
seems there is a slight mismatch between @cloudflare/workers-types/2024-04-05 and 4.20240405.0"