Hoppa till huvudinnehållet
Version: 11.x

Härleda Typer

Inofficiell Beta-översättning

Denna sida har översatts av PageTurner AI (beta). Inte officiellt godkänd av projektet. Hittade du ett fel? Rapportera problem →

Det är ofta användbart att komma åt dina API:ers typer i dina klienter. För detta ändamål kan du härleda typerna som finns i din AppRouter.

@trpc/server exporterar följande hjälptyper för att underlätta härledning av dessa typer från AppRouter som exporteras av din @trpc/server-router:

  • inferRouterInputs<TRouter>

  • inferRouterOutputs<TRouter>

  • inferProcedureInput<TProcedure>

  • inferProcedureOutput<TProcedure>

  • inferSubscriptionInput<TProcedure>

  • inferSubscriptionOutput<TProcedure>

Härleda Indata- och Utdatatyper

Låt oss anta att vi har denna exempelrouter:

server.ts
ts
import { initTRPC } from '@trpc/server';
import { z } from "zod";
 
const t = initTRPC.create();
 
const appRouter = t.router({
post: t.router({
list: t.procedure
.query(() => {
// imaginary db call
return [{ id: 1, title: 'tRPC is the best!' }];
}),
byId: t.procedure
.input(z.string())
.query((opts) => {
// imaginary db call
return { id: 1, title: 'tRPC is the best!' };
}),
create: t.procedure
.input(z.object({ title: z.string(), text: z.string(), }))
.mutation((opts) => {
// imaginary db call
return { id: 1, ...opts.input };
}),
onPostAdd: t.procedure
.input(z.object({ authorId: z.string() }))
.subscription(async function* ({ input }) {
// imaginary event source
yield {
id: 1,
title: 'tRPC is the best!',
authorId: input.authorId,
};
}),
}),
});
 
export type AppRouter = typeof appRouter;
server.ts
ts
import { initTRPC } from '@trpc/server';
import { z } from "zod";
 
const t = initTRPC.create();
 
const appRouter = t.router({
post: t.router({
list: t.procedure
.query(() => {
// imaginary db call
return [{ id: 1, title: 'tRPC is the best!' }];
}),
byId: t.procedure
.input(z.string())
.query((opts) => {
// imaginary db call
return { id: 1, title: 'tRPC is the best!' };
}),
create: t.procedure
.input(z.object({ title: z.string(), text: z.string(), }))
.mutation((opts) => {
// imaginary db call
return { id: 1, ...opts.input };
}),
onPostAdd: t.procedure
.input(z.object({ authorId: z.string() }))
.subscription(async function* ({ input }) {
// imaginary event source
yield {
id: 1,
title: 'tRPC is the best!',
authorId: input.authorId,
};
}),
}),
});
 
export type AppRouter = typeof appRouter;

Med hjälp av dessa hjälptyper kan vi härleda typerna för vår router. Följande exempel visar hur man härleder typerna för post.create-proceduren:

client.ts
ts
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
import type { AppRouter } from './server';
 
type RouterInput = inferRouterInputs<AppRouter>;
type RouterOutput = inferRouterOutputs<AppRouter>;
 
type PostCreateInput = RouterInput['post']['create'];
type PostCreateOutput = RouterOutput['post']['create'];
client.ts
ts
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
import type { AppRouter } from './server';
 
type RouterInput = inferRouterInputs<AppRouter>;
type RouterOutput = inferRouterOutputs<AppRouter>;
 
type PostCreateInput = RouterInput['post']['create'];
type PostCreateOutput = RouterOutput['post']['create'];

Härleda typer för enskilda procedurer

Om du redan har tillgång till en specifik procedur i din router kan du härleda dess indata eller utdata direkt:

client.ts
ts
import type {
inferProcedureInput,
inferProcedureOutput,
} from '@trpc/server';
import type { AppRouter } from './server';
 
type PostByIdInput = inferProcedureInput<AppRouter['post']['byId']>;
type PostByIdOutput = inferProcedureOutput<AppRouter['post']['byId']>;
client.ts
ts
import type {
inferProcedureInput,
inferProcedureOutput,
} from '@trpc/server';
import type { AppRouter } from './server';
 
type PostByIdInput = inferProcedureInput<AppRouter['post']['byId']>;
type PostByIdOutput = inferProcedureOutput<AppRouter['post']['byId']>;

För prenumerationer kan du härleda prenumerationsindatan och den emitterade datatypen:

client.ts
ts
import type {
inferSubscriptionInput,
inferSubscriptionOutput,
} from '@trpc/server';
import type { AppRouter } from './server';
 
type OnPostAddInput = inferSubscriptionInput<AppRouter['post']['onPostAdd']>;
type OnPostAddOutput = inferSubscriptionOutput<AppRouter['post']['onPostAdd']>;
client.ts
ts
import type {
inferSubscriptionInput,
inferSubscriptionOutput,
} from '@trpc/server';
import type { AppRouter } from './server';
 
type OnPostAddInput = inferSubscriptionInput<AppRouter['post']['onPostAdd']>;
type OnPostAddOutput = inferSubscriptionOutput<AppRouter['post']['onPostAdd']>;

Härled TRPCClientError-typer

Det är också användbart att härleda feltypen för din AppRouter

client.ts
ts
import { TRPCClientError } from '@trpc/client';
import type { AppRouter } from './server';
import { trpc } from './trpc';
 
export function isTRPCClientError(
cause: unknown,
): cause is TRPCClientError<AppRouter> {
return cause instanceof TRPCClientError;
}
 
async function main() {
try {
await trpc.post.byId.query('1');
} catch (cause) {
if (isTRPCClientError(cause)) {
// `cause` is now typed as your router's `TRPCClientError`
console.log('data', cause.data);
} else {
// [...]
}
}
}
 
main();
client.ts
ts
import { TRPCClientError } from '@trpc/client';
import type { AppRouter } from './server';
import { trpc } from './trpc';
 
export function isTRPCClientError(
cause: unknown,
): cause is TRPCClientError<AppRouter> {
return cause instanceof TRPCClientError;
}
 
async function main() {
try {
await trpc.post.byId.query('1');
} catch (cause) {
if (isTRPCClientError(cause)) {
// `cause` is now typed as your router's `TRPCClientError`
console.log('data', cause.data);
} else {
// [...]
}
}
}
 
main();