Repository navigation
Description
Describe the feature you'd like to request
So one problem I've noticed that a bunch of people run into with MonoRepo Structures, they are trying to define their types in a module to import into their different services (backend, frontend, other packages) and end up with circular dependencies.
They end up importing the AppRouter as a Type from their backend, and since that backend depends on the types defined in their shared type package, they now have a circular dependency.
I understand they could create a seperate package specifically for shared router type definition or import the router from the server in order to use in the other services to avoid this problem, but that can often be considered unpleasant in intra-package-dependencies.
So, I was thinking it would be nice if there was a seperate method for going about router definition.
Describe the solution you'd like to see
So I really see two ways to go about fixing this. I believe the easiest solution would constructing a CLI DevTool that allows the user to run a generate router definition command where you can specify a location to create a generated file for their router. This could mimic the way that Encore.dev goes about Client Generation, however a little more focused in on just the type (at least in an initial release of the tool).
This also might not be easier than the second method due to the fact that you actually have to code a CLI...
Describe alternate solutions
So instead of a CLI, if @trpc/Server had a function or method exposed that allows the user to construct the Router Type by passing in constructed Object of Record<procedureName, ProcedureOpts> where you define the input & output types, type of procedure, and the Meta... That could prove very useful. Plus you can just define all these types in a shared Type Package, and the backend could take in the type and you wouldn't have to explicitly declare the input and output types of those endpoints.
Doing it this way also would allow us to be able to use UnionType Routers for the backend, where we could create seperate Router Type Definitions to use with different clients to decide what endpoints are exposed to different clients and prevent overexposure of endpoints in more granular instances.
Additional information
No response
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR implementing this feature!
Activity
KATT commentedon Jun 26, 2025
Would love it if you have a play with!
I'm not sure if it's solvable without a CLI-tool as I think that the
.d.tswill still "leak" info about context etc (eg #6830)darkship commentedon Jul 10, 2025
This would probably also solve my issue where
createTRPCClient<AppRouter>requires to export types fromContextwhich is actually not needed.In my case, I use fastify and it requires me to re-export some of it types because my context includes
resandreqwhen I build my client package:jamiechong commentedon Jul 10, 2025
Not sure if this is helpful, but I've been using this to get a flat file that defines my Router that can be used in the client. We need this because we're not using a mono-repo. The output file still includes some dependencies on the server though. Such as:
import("@trpc/server").TRPCQueryProcedure, etc.KATT commentedon Nov 2, 2026
🤖 v12 has contract-first routers (experimental):
experimental_contractdescribes each procedure's input, output and errors in a package of its own, the server implements it witht.implement(contract), which checks that every procedure is implemented, and a client takes the contract as itsrouter. The frontend and other packages then depend only on the contract, never on the backend, so the circular dependency and the leaked context types go away. https://v12.trpc.io/v12/docs/server/contract-first#clients-from-a-contract. v12 isn't released yet and there's no ETA.