Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Durable Object RPC Type Errors #1996

Closed
davidhollenbeckx opened this issue Apr 9, 2024 · 1 comment · Fixed by #2034
Closed

Durable Object RPC Type Errors #1996

davidhollenbeckx opened this issue Apr 9, 2024 · 1 comment · Fixed by #2034
Assignees
Labels
types Related to @cloudflare/workers-types

Comments

@davidhollenbeckx
Copy link

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>

@davidhollenbeckx davidhollenbeckx added the types Related to @cloudflare/workers-types label Apr 9, 2024
@davidhollenbeckx
Copy link
Author

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types Related to @cloudflare/workers-types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants