推断类型
在客户端中访问 API 类型通常很有用。为此,你可以推断 AppRouter
.h 中包含的类型。
¥It is often useful to access the types of your API within your clients. For this purpose, you are able to infer the types contained in your AppRouter
.
@trpc/server
导出以下辅助程序类型,以帮助从 @trpc/server
路由导出的 AppRouter
推断这些类型:
¥@trpc/server
exports the following helper types to assist with inferring these types from the AppRouter
exported by your @trpc/server
router:
inferRouterInputs<TRouter>
inferRouterOutputs<TRouter>
推断输入和输出类型
¥Inferring Input & Output Types
假设我们有这个示例路由:
¥Let's assume we have this example router:
server.tsts
// @filename: server.tsimport {initTRPC } from '@trpc/server';import {z } from "zod";constt =initTRPC .create ();constappRouter =t .router ({post :t .router ({list :t .procedure .query (() => {// imaginary db callreturn [{id : 1,title : 'tRPC is the best!' }];}),byId :t .procedure .input (z .string ()).query ((opts ) => {// imaginary db callreturn {id : 1,title : 'tRPC is the best!' };}),create :t .procedure .input (z .object ({title :z .string (),text :z .string (), })).mutation ((opts ) => {// imaginary db callreturn {id : 1, ...opts .input };}),}),});export typeAppRouter = typeofappRouter ;
server.tsts
// @filename: server.tsimport {initTRPC } from '@trpc/server';import {z } from "zod";constt =initTRPC .create ();constappRouter =t .router ({post :t .router ({list :t .procedure .query (() => {// imaginary db callreturn [{id : 1,title : 'tRPC is the best!' }];}),byId :t .procedure .input (z .string ()).query ((opts ) => {// imaginary db callreturn {id : 1,title : 'tRPC is the best!' };}),create :t .procedure .input (z .object ({title :z .string (),text :z .string (), })).mutation ((opts ) => {// imaginary db callreturn {id : 1, ...opts .input };}),}),});export typeAppRouter = typeofappRouter ;
使用辅助程序,我们可以推断路由的类型。以下示例显示如何推断 post.create
过程的类型:
¥Using the helpers, we can infer the types of our router. The following example shows how to infer the types of the post.create
procedure:
client.tsts
// @filename: client.tsimport type {inferRouterInputs ,inferRouterOutputs } from '@trpc/server';import type {AppRouter } from './server';typeRouterInput =inferRouterInputs <AppRouter >;typeRouterOutput =inferRouterOutputs <AppRouter >;typePostCreateInput =RouterInput ['post']['create'];typePostCreateOutput =RouterOutput ['post']['create'];
client.tsts
// @filename: client.tsimport type {inferRouterInputs ,inferRouterOutputs } from '@trpc/server';import type {AppRouter } from './server';typeRouterInput =inferRouterInputs <AppRouter >;typeRouterOutput =inferRouterOutputs <AppRouter >;typePostCreateInput =RouterInput ['post']['create'];typePostCreateOutput =RouterOutput ['post']['create'];
推断 TRPCClientError
类型
¥Infer TRPCClientError
types
推断 AppRouter
的错误类型也很有用
¥It's also useful to infer the error type for your AppRouter
client.tsts
// @filename: client.tsimport {TRPCClientError } from '@trpc/client';import type {AppRouter } from './server';import {trpc } from './trpc';export functionisTRPCClientError (cause : unknown,):cause isTRPCClientError <AppRouter > {returncause instanceofTRPCClientError ;}async functionmain () {try {awaittrpc .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.tsts
// @filename: client.tsimport {TRPCClientError } from '@trpc/client';import type {AppRouter } from './server';import {trpc } from './trpc';export functionisTRPCClientError (cause : unknown,):cause isTRPCClientError <AppRouter > {returncause instanceofTRPCClientError ;}async functionmain () {try {awaittrpc .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 ();