타입 추론
비공식 베타 번역
이 페이지는 PageTurner AI로 번역되었습니다(베타). 프로젝트 공식 승인을 받지 않았습니다. 오류를 발견하셨나요? 문제 신고 →
@trpc/server에서 제공하는 타입 추론(여기 참조)에 더해, 이 통합(React용)은 순수하게 React 환경에서 사용하기 위한 추가적인 추론 도우미를 제공합니다.
라우터 기반 React Query 옵션 추론
tRPC 프로시저 주변의 커스텀 훅을 생성할 때, 라우터에서 옵션 타입을 자동으로 추론해야 하는 경우가 있습니다. @trpc/react-query에서 내보내는 inferReactQueryProcedureOptions 도우미를 사용하면 이를 구현할 수 있습니다.
trpc.tstsimport {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.tstsimport {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.tstsimport {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.tstsimport {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.tstsimport {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.tstsimport {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는 라우터 팩토리에 대한 추상 타입을 생성하고, 라우터를 prop으로 전달받는 공통 React 컴포넌트를 구축하는 데 사용할 수 있는 여러 타입을 제공합니다.
api/factory.tstsximport {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.tstsximport {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.tstsxexport typeAppRouter = typeofappRouter ;// Export your MyRouter types to the clientexport type {MyRouterLike ,MyRouterUtilsLike } from './factory';
api/server.tstsxexport typeAppRouter = typeofappRouter ;// Export your MyRouter types to the clientexport type {MyRouterLike ,MyRouterUtilsLike } from './factory';
frontend/usePostCreate.tstsximport 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.tstsximport 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 }/>);}
더 완벽한 실제 구현 예제는 여기에서 확인할 수 있습니다