Course contentsShow
← TypeScript
Lesson 1544 of 1,79635. TypeScript with Framework Cross-ReferencesFree lesson

Mutation Procedures with Input Types

Building mutation procedures with typed inputs that provide autocomplete and type safety across the stack.

Mutation Procedures with Input Types

What you'll learn: How to build type-safe mutation procedures in tRPC where input validation schemas automatically provide autocomplete and type safety from client to server.


What Are Mutation Procedures?

In tRPC, mutations are procedures that modify server state—creating, updating, or deleting data. Unlike queries (which fetch data), mutations typically accept structured input data and perform side effects.

The breakthrough with TypeScript and tRPC is that your input validation schema becomes your type definition. When you define a Zod schema for your mutation input, TypeScript infers the exact shape, giving you autocomplete and compile-time safety across your entire stack.


Building a Typed Mutation

When you create a mutation procedure, you use .input() to specify a Zod schema. That schema validates runtime data and generates TypeScript types:

import { z } from 'zod';
import { router, publicProcedure } from './trpc';

const appRouter = router({
  createUser: publicProcedure
    .input(z.object({
      email: z.string().email(),
      name: z.string().min(2),
      age: z.number().int().positive().optional()
    }))
    .mutation(async ({ input }) => {
      // input is fully typed!
      // input.email is string
      // input.name is string
      // input.age is number | undefined
      
      return { id: 123, ...input };
    })
});

On the client side, when you call this mutation, TypeScript knows exactly what shape the input must have. You get autocomplete for email, name, and age, and TypeScript will error if you pass invalid properties or wrong types.


The Type Safety Flow

Think of it like a contract written once, enforced everywhere:

  1. Define the schema with Zod (runtime validation)
  2. TypeScript infers types from that schema automatically
  3. Server receives validated input with full type information
  4. Client must provide correct input or TypeScript errors at compile time

This eliminates the traditional gap where your API documentation says one thing but runtime behavior differs. The schema is the documentation, the validator, and the type.


Complex Input Shapes

You can build sophisticated input types with nested objects, arrays, unions, and optional fields—all with full type inference:

const updatePost = publicProcedure
  .input(z.object({
    postId: z.number(),
    title: z.string().optional(),
    tags: z.array(z.string()).optional(),
    metadata: z.object({
      published: z.boolean(),
      featured: z.boolean()
    }).optional()
  }))
  .mutation(async ({ input }) => {
    // Every property autocompletes correctly
    // Optional properties are typed as T | undefined
  });

When calling this from your frontend, you'll see autocomplete for postId, title, tags, and even nested metadata.published. TypeScript prevents typos, wrong types, and missing required fields before you even run the code.


Key takeaway: Mutation procedures with Zod input schemas give you end-to-end type safety where a single schema definition provides both runtime validation and compile-time TypeScript types across your entire application stack, with zero code generation required.