推断类型
除了 @trpc/server
提供的类型推断(请参阅此处),此集成还提供了一些仅在 React 中使用的推断助手。
根据路由器推断 React Query 选项
在围绕 tRPC 过程创建自定义挂钩时,有时需要从路由器推断选项的类型。您可以通过从 @trpc/react-query
导出的 inferReactQueryProcedureOptions
助手来实现。
trpc.tsts
import {createTRPCReact ,typeinferReactQueryProcedureOptions ,} from '@trpc/react-query';import type {inferRouterInputs ,inferRouterOutputs } from '@trpc/server';import type {AppRouter } from './server';// infer the types for your routerexport typeReactQueryOptions =inferReactQueryProcedureOptions <AppRouter >;export typeRouterInputs =inferRouterInputs <AppRouter >;export typeRouterOutputs =inferRouterOutputs <AppRouter >;export consttrpc =createTRPCReact <AppRouter >();
trpc.tsts
import {createTRPCReact ,typeinferReactQueryProcedureOptions ,} from '@trpc/react-query';import type {inferRouterInputs ,inferRouterOutputs } from '@trpc/server';import type {AppRouter } from './server';// infer the types for your routerexport typeReactQueryOptions =inferReactQueryProcedureOptions <AppRouter >;export typeRouterInputs =inferRouterInputs <AppRouter >;export typeRouterOutputs =inferRouterOutputs <AppRouter >;export consttrpc =createTRPCReact <AppRouter >();
usePostCreate.tsts
import {trpc ,typeReactQueryOptions ,typeRouterInputs ,typeRouterOutputs ,} from './trpc';typePostCreateOptions =ReactQueryOptions ['post']['create'];functionusePostCreate (options ?:PostCreateOptions ) {constutils =trpc .useUtils ();returntrpc .post .create .useMutation ({...options ,onSuccess (post ) {// invalidate all queries on the post router// when a new post is createdutils .post .invalidate ();options ?.onSuccess ?.(post );},});}
usePostCreate.tsts
import {trpc ,typeReactQueryOptions ,typeRouterInputs ,typeRouterOutputs ,} from './trpc';typePostCreateOptions =ReactQueryOptions ['post']['create'];functionusePostCreate (options ?:PostCreateOptions ) {constutils =trpc .useUtils ();returntrpc .post .create .useMutation ({...options ,onSuccess (post ) {// invalidate all queries on the post router// when a new post is createdutils .post .invalidate ();options ?.onSuccess ?.(post );},});}
usePostById.tsts
import {ReactQueryOptions ,RouterInputs ,trpc } from './trpc';typePostByIdOptions =ReactQueryOptions ['post']['byId'];typePostByIdInput =RouterInputs ['post']['byId'];functionusePostById (input :PostByIdInput ,options ?:PostByIdOptions ) {returntrpc .post .byId .useQuery (input ,options );}
usePostById.tsts
import {ReactQueryOptions ,RouterInputs ,trpc } from './trpc';typePostByIdOptions =ReactQueryOptions ['post']['byId'];typePostByIdInput =RouterInputs ['post']['byId'];functionusePostById (input :PostByIdInput ,options ?:PostByIdOptions ) {returntrpc .post .byId .useQuery (input ,options );}
从“路由器工厂”推断抽象类型
如果你在应用程序中多次编写一个创建类似路由器界面的工厂,你可能希望在工厂的使用之间共享客户端代码。@trpc/react-query/shared
导出了多种类型,这些类型可用于为路由器工厂生成抽象类型,并构建将路由器作为道具传递的通用 React 组件。
api/factory.tstsx
import {t ,publicProcedure } from './trpc';// @trpc/react-query/shared exports several **Like types which can be used to generate abstract typesimport {RouterLike ,UtilsLike } from '@trpc/react-query/shared';// Factory function written by you, however you need,// so long as you can infer the resulting type of t.router() laterexport functioncreateMyRouter () {returnt .router ({createThing :publicProcedure .input (ThingRequest ).output (Thing ).mutation (/* do work */),listThings :publicProcedure .input (ThingQuery ).output (ThingArray ).query (/* do work */),})}// Infer the type of your router, and then generate the abstract types for use in the clienttypeMyRouterType =ReturnType <typeofcreateMyRouter >exportMyRouterLike =RouterLike <MyRouterType >exportMyRouterUtilsLike =UtilsLike <MyRouterType >
api/factory.tstsx
import {t ,publicProcedure } from './trpc';// @trpc/react-query/shared exports several **Like types which can be used to generate abstract typesimport {RouterLike ,UtilsLike } from '@trpc/react-query/shared';// Factory function written by you, however you need,// so long as you can infer the resulting type of t.router() laterexport functioncreateMyRouter () {returnt .router ({createThing :publicProcedure .input (ThingRequest ).output (Thing ).mutation (/* do work */),listThings :publicProcedure .input (ThingQuery ).output (ThingArray ).query (/* do work */),})}// Infer the type of your router, and then generate the abstract types for use in the clienttypeMyRouterType =ReturnType <typeofcreateMyRouter >exportMyRouterLike =RouterLike <MyRouterType >exportMyRouterUtilsLike =UtilsLike <MyRouterType >
api/server.tstsx
export typeAppRouter = typeofappRouter ;// Export your MyRouter types to the clientexport type {MyRouterLike ,MyRouterUtilsLike } from './factory';
api/server.tstsx
export typeAppRouter = typeofappRouter ;// Export your MyRouter types to the clientexport type {MyRouterLike ,MyRouterUtilsLike } from './factory';
frontend/usePostCreate.tstsx
import type {MyRouterLike ,MyRouterUtilsLike ,trpc ,useUtils } from './trpc';typeMyGenericComponentProps = {route :MyRouterLike ;utils :MyRouterUtilsLike ;};functionMyGenericComponent (props :MyGenericComponentProps ) {const {route } =props ;constthing =route .listThings .useQuery ({filter : 'qwerty',});constmutation =route .doThing .useMutation ({onSuccess () {props .utils .listThings .invalidate ();},});functionhandleClick () {mutation .mutate ({name : 'Thing 1',});}return; /* ui */}functionMyPageComponent () {constutils =useUtils ();return (<MyGenericComponent route ={trpc . deep .route .things }utils ={utils . deep .route .things }/>);}functionMyOtherPageComponent () {constutils =useUtils ();return (<MyGenericComponent route ={trpc . different .things }utils ={utils . different .things }/>);}
frontend/usePostCreate.tstsx
import type {MyRouterLike ,MyRouterUtilsLike ,trpc ,useUtils } from './trpc';typeMyGenericComponentProps = {route :MyRouterLike ;utils :MyRouterUtilsLike ;};functionMyGenericComponent (props :MyGenericComponentProps ) {const {route } =props ;constthing =route .listThings .useQuery ({filter : 'qwerty',});constmutation =route .doThing .useMutation ({onSuccess () {props .utils .listThings .invalidate ();},});functionhandleClick () {mutation .mutate ({name : 'Thing 1',});}return; /* ui */}functionMyPageComponent () {constutils =useUtils ();return (<MyGenericComponent route ={trpc . deep .route .things }utils ={utils . deep .route .things }/>);}functionMyOtherPageComponent () {constutils =useUtils ();return (<MyGenericComponent route ={trpc . different .things }utils ={utils . different .things }/>);}
一个更完整的示例 可以在这里找到