Course contentsShow
← TypeScript
Lesson 1564 of 1,79636. tRPC and End-to-End Type SafetyFree lesson

Type Inference Across Query Procedures

See how TypeScript infers input schemas and return types automatically from query definitions to client usage.

Type Inference Across Query Procedures

What you'll learn: How TypeScript automatically infers input schemas and return types from tRPC query definitions all the way to client usage, with zero code generation.

The Magic of End-to-End Type Flow

When you define a tRPC query procedure on your server, TypeScript captures both what inputs it accepts (via Zod schemas) and what data it returns. This type information then flows automatically to your client code—no build step, no code generation, just pure TypeScript inference.

Think of it like a chain reaction: the server's procedure definition is the first domino, and when you call that procedure from your client, TypeScript knocks down every domino in between, ensuring your input matches the schema and your result matches the return type.

How Schema Types Flow to the Client

On the server side, when you attach a Zod schema to a query's input, TypeScript extracts the type from that schema. When you return data from your procedure, TypeScript infers the return type from your actual implementation.

On the client side, when you call client.procedureName.query(), TypeScript knows:

  • What shape of data you must pass as input (inferred from the Zod schema)
  • What shape of data you'll receive back (inferred from the procedure's return type)

This happens because your tRPC router type is imported into your client code, and the createTRPCProxyClient uses that router type as a generic parameter. The TypeScript compiler traces through the router definition to match procedure names with their input and output types.

The Single Import That Powers Everything

You only import your router type (not the runtime implementation) into your client. This type contains all procedure signatures, input requirements, and return types. When you access a procedure through the client proxy, TypeScript looks up that procedure in the router type and applies the correct input/output types to your specific call.

There's no magic at runtime—the client sends JSON over HTTP like any other API. But at compile time, TypeScript ensures your inputs are valid and your handling of responses is safe, all from that single router type reference.

Key Takeaway: tRPC's type inference works by importing your server's router type to the client, allowing TypeScript to automatically enforce input schemas and return types across procedure calls without any code generation.