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

rpc can only return type not interface/class #2003

Open
helloimalastair opened this issue Apr 10, 2024 · 5 comments · May be fixed by #2040
Open

rpc can only return type not interface/class #2003

helloimalastair opened this issue Apr 10, 2024 · 5 comments · May be fixed by #2040
Assignees
Labels
types Related to @cloudflare/workers-types

Comments

@helloimalastair
Copy link

Repro

When calling a DO RPC method, return type collapses to Promise<undefined>/never if using an interface. When using a type, return values are as expected.

@helloimalastair helloimalastair added the types Related to @cloudflare/workers-types label Apr 10, 2024
@helloimalastair helloimalastair changed the title rpc can only return type not interface rpc can only return type not interface/class Apr 12, 2024
@helloimalastair
Copy link
Author

Just ran into it not working with WebSockets either, which are classes, though not sure you can move a WebSocket anyway...

@kentonv
Copy link
Member

kentonv commented Apr 15, 2024

As discussed, it's expected (and documented) that class instances are only supported if they extend RpcTarget or are instances of one of the specific API types that are documented to work. WebSocket is not currently supported.

That said, it sounds like there is arguably a bug in the TypeScript types. It sounds like the types treat interface as strictly referring to class instances, but actually an interface can be satisfied by a plain object too.

@mrbbot Is there any way for us to more specifically filter out class instances using the TypeScript type system?

@kentonv
Copy link
Member

kentonv commented Apr 15, 2024

Err, apparently @mrbbot recently left the company. Sorry for the spurious ping @mrbbot, hope things go well at whatever's next for you.

I'm not sure who that leaves as the typescript wizard now. @GregBrimble any thoughts?

@katis
Copy link

katis commented Apr 20, 2024

Recursively mapping the return type to a type alias helped me work around this issue with some 3rd party types

type InterfaceToType<T> = T extends object ? { [P in keyof T]: InterfaceToType<T[P]> } : T

class Foo extends RpcTarget {
	async bar(): Promise<InterfaceToType<InterfaceOfThirdParty>> {
            // ...
        }
}

@GregBrimble
Copy link
Member

@penalosa would be my next go-to!

alpertuna added a commit to alpertuna/workerd that referenced this issue Apr 20, 2024
@alpertuna alpertuna linked a pull request Apr 20, 2024 that will close this issue
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
Status: Untriaged
Development

Successfully merging a pull request may close this issue.

5 participants