See example repo (https://github.com/rhinodavid/trpc-server-definition) for all the details
My request is similar to the one @iduuck wrote about in #3496 (comment). tldr: One should not need the server implementation in order to get a typed client. Currently this is not possible (I don't think -- I"m pretty new here) -- all the examples I've seen create the client using the typeof
the AppRouter definition.
My proposed solution is to create utility types to define the router type. Usage would look like this:
// appRouterInterface.ts
import type { defineRouter, defineQueryProcedure } from "@trpc/server";
export type TAppRouter = defineRouter<{
greet: defineQueryProcedure<{name: string}, {greeting: string}>;
}>
In the server implementation, you'd enforce that the server satisfies the interface:
// server.ts
// ...
import type { TAppRouter } from "./appRouterInterface";
// this could be published as an open source package ^^
export const appRouter = t.router({ /* ... */ }) satisfies TAppRouter;
Then the client is typed with the interface:
// client.ts
import type { TAppRouter } from "./appRouterInterface";
export const client = createTRPCProxyClient<TAppRouter>({ /* ... */ });
The example repo has a complete(?) working PoC.
Another possible solution is a codegen step as described in #3496 (comment).
The pro of this solution is it keeps the existing pattern of
[implement router] -> [extract interface].
Some might prefer this, but after a long time using gRPC/stubby I'm more used to the [define interface] -> [implement router] pattern. This pattern is better for large organizations where multiple teams might need to debate and agree on the interface.
The con of the solution is codegen can be finicky. It also doesn't easily solve the workspace organization problem I describe in my example repo readme.
No response
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too