Skip to content

feat: AppRouter Type Generation #6835

Open

Description

@ThreeCrown

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).

https://encore.dev/docs/ts/cli/client-generation

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

  1. KATT commented on Jun 26, 2025

    @KATT
    Member

    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.ts will still "leak" info about context etc (eg #6830)

  2. darkship commented on Jul 10, 2025

    @darkship

    This would probably also solve my issue where createTRPCClient<AppRouter> requires to export types from Context which 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 res and req when I build my client package:

    TS2742: The inferred type of createClient cannot be named without a reference to ../../service/node_modules/fastify/fastify. This is likely not portable. A type annotation is necessary.
    TS2742: The inferred type of createClient cannot be named without a reference to
    ../../service/node_modules/fastify/types/type-provider
    . This is likely not portable. A type annotation is necessary.
    
    // In the server package
    
    // context.ts
    export function createContext({ req, res }: CreateFastifyContextOptions) {
      return {
        req,
        res
      };
    }
    
    export type Context = Awaited<ReturnType<typeof createContext>>;
    
    // router.ts
    [...]
    export type AppRouter = typeof appRouter;
    
    
    // In the client package
    import type { AppRouter } from 'server';
    
    export const createClient = (url: string | URL) =>
      createTRPCClient<AppRouter>({
        links: [
          httpBatchLink({
            transformer: superjson,
            url,
          }),
        ],
      });
    
  3. jamiechong commented on Jul 10, 2025

    @jamiechong

    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.

    package.json (scripts):
    "build:types": "dts-bundle-generator --config dts.config.json",
    
    // dts.config.js:
    {
      "compilationOptions": {
        "preferredConfigPath": "./tsconfig.json"
      },
      "entries": [
        {
          "filePath": "./src/router/router.ts",
          "outFile": "./build/client/types/router.d.ts",
          "output": {
            "noBanner": true,
            "inlineDeclareGlobals": true,
            "inlineDeclareExternals": true,
            "respectPreserveConstEnum": true,
            "exportReferencedTypes": true
          },
          "libraries": {
            "inlinedLibraries": [],
            "importedLibraries": []
          }
        }
      ]
    }
    
  4. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 v12 has contract-first routers (experimental): experimental_contract describes each procedure's input, output and errors in a package of its own, the server implements it with t.implement(contract), which checks that every procedure is implemented, and a client takes the contract as its router. 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.

  5. added
    next-major/maybe
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-major/maybe

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions