Skip to main content

Schema Generation

nest-trpc-native can auto-generate a TypeScript file exporting your AppRouter type for end-to-end type safety.

Enable Schema Generation

Set the autoSchemaFile option:

TrpcModule.forRoot({
path: '/trpc',
autoSchemaFile: 'src/@generated/server.ts',
});

On application startup, this generates a file like:

src/@generated/server.ts
// Auto-generated by nest-trpc-native — do not edit
import type { AppRouter } from '../app.module';
export type { AppRouter };

Client Usage

Import the generated type to create a fully typed client:

import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
import type { AppRouter } from './@generated/server';

const trpc = createTRPCProxyClient<AppRouter>({
links: [httpBatchLink({ url: 'http://localhost:3000/trpc' })],
});

// Full autocompletion and type checking
const cats = await trpc.cats.list.query();
const user = await trpc.users.create.mutate({ name: 'Ada' });

Compile-Time Client Checking

Create a typecheck-only file to ensure your client calls stay in sync:

src/client.typecheck.ts
import { createTRPCProxyClient } from '@trpc/client';
import type { AppRouter } from './@generated/server';

// This file is never executed — only type-checked
declare const trpc: ReturnType<typeof createTRPCProxyClient<AppRouter>>;

// These will fail at compile time if the API changes
trpc.cats.list.query();
trpc.users.create.mutate({ name: 'test' });
npx tsc --noEmit -p tsconfig.client.json

Add the generated file to .gitignore:

src/@generated/