The Router Type: AppRouter as Source of Truth
What you'll learn: How exporting and importing your tRPC router type enables automatic type inference in client code, creating a single source of truth for your API contract.
The Type Export Pattern
In tRPC, your backend router isn't just runtime code—it's also a type definition that describes your entire API surface. By exporting the router's type, client code can infer every procedure's input and output automatically.
Think of it like this: instead of maintaining two separate contracts (one on the server, one documented for the client), you export a type that is the contract. The client imports that type and gets instant autocomplete and type-checking for every endpoint.
Basic Pattern
On your backend:
// server/router.ts
import { initTRPC } from '@trpc/server';
const t = initTRPC.create();
export const appRouter = t.router({
getUser: t.procedure.query(() => {
return { id: 1, name: 'Alice' };
}),
createPost: t.procedure.mutation(() => {
return { postId: 42 };
})
});
// Export ONLY the type, not the implementation
export type AppRouter = typeof appRouter;
On your frontend:
// client/trpc.ts
import { createTRPCProxyClient } from '@trpc/client';
import type { AppRouter } from '../server/router';
const client = createTRPCProxyClient<AppRouter>({
// configuration...
});
// Now TypeScript knows getUser returns { id: number; name: string }
const user = await client.getUser.query();
Why This Matters
The AppRouter type captures everything: procedure names, whether they're queries or mutations, input schemas, and output types. When the client imports type AppRouter, it doesn't import any server code—just the type information. This keeps your bundle small while giving you full type safety.
Key Takeaway: Exporting type AppRouter = typeof appRouter creates a type-only bridge between server and client, enabling end-to-end type inference without code generation or runtime dependencies.