How tRPC Propagates Types Across the Network Boundary
What you'll learn: How tRPC uses TypeScript's type inference to share server types with client code without runtime overhead.
When you make an API call, you're crossing a network boundary—sending data from client to server or vice versa. Normally, TypeScript can't help you here because types don't exist at runtime. They disappear after compilation.
tRPC's magic is that it never sends types across the network. Instead, it uses TypeScript's type system as a "bridge" that exists only at compile time.
The Key Mechanism: Import-Based Type Flow
When you set up tRPC, your client code imports the router type from your server code:
// Server: api/router.ts
export const appRouter = router({
getUser: query(/* ... */),
createPost: mutation(/* ... */)
});
export type AppRouter = typeof appRouter;
// Client: somewhere else in your codebase
import type { AppRouter } from '../server/api/router';
const client = createTRPCClient<AppRouter>(/* ... */);
The client never imports the implementation—only the type. This is a type-only import, so it produces zero runtime code.
Inference Magic
Because the client has access to AppRouter's type definition, TypeScript can:
- Know what procedures exist (queries and mutations)
- Infer input types from Zod schemas you defined server-side
- Infer return types from what your procedures return
- Provide autocomplete for procedure names and parameters
All of this happens through pure type inference—TypeScript analyzing the shape of your router at compile time.
The Network Only Carries JSON
At runtime, tRPC sends regular JSON over HTTP (or WebSockets). The types never cross the wire. They exist only in your IDE and during type-checking.
This is why tRPC is often called "zero-overhead type safety"—there's no runtime type information, no serialized schemas, no validation payloads (beyond what Zod does for runtime safety).
Key Takeaway: tRPC doesn't send types across the network—it uses TypeScript's import system to let the client "see" the server's type definitions at compile time, enabling full type inference without runtime cost.