推断类型
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
在封装 @trpc/client 或 @trpc/react API 功能时,通常需要推断由 @trpc/server 路由器生成的输入类型、输出类型和 API 路径。
推断辅助工具
@trpc/server 提供以下辅助类型,帮助从你的 @trpc/server 路由器导出的 AppRouter 中推断这些类型:
-
inferProcedureOutput<TProcedure> -
inferProcedureInput<TProcedure> -
inferSubscriptionOutput<TRouter, TPath>
示例代码
utils/trpc.tstsx// trpc-helper.ts// Import AppRouter from your main server routerimport type {inferProcedureInput,inferProcedureOutput,inferSubscriptionOutput,} from '@trpc/server';import type { AppRouter } from 'api/src/routers/_app';/*** Enum containing all api query paths*/export type TQuery = keyof AppRouter['_def']['queries'];/*** Enum containing all api mutation paths*/export type TMutation = keyof AppRouter['_def']['mutations'];/*** Enum containing all api subscription paths*/export type TSubscription = keyof AppRouter['_def']['subscriptions'];/*** This is a helper method to infer the output of a query resolver* @example type HelloOutput = InferQueryOutput<'hello'>*/export type InferQueryOutput<TRouteKey extends TQuery> = inferProcedureOutput<AppRouter['_def']['queries'][TRouteKey]>;/*** This is a helper method to infer the input of a query resolver* @example type HelloInput = InferQueryInput<'hello'>*/export type InferQueryInput<TRouteKey extends TQuery> = inferProcedureInput<AppRouter['_def']['queries'][TRouteKey]>;/*** This is a helper method to infer the output of a mutation resolver* @example type HelloOutput = InferMutationOutput<'hello'>*/export type InferMutationOutput<TRouteKey extends TMutation> =inferProcedureOutput<AppRouter['_def']['mutations'][TRouteKey]>;/*** This is a helper method to infer the input of a mutation resolver* @example type HelloInput = InferMutationInput<'hello'>*/export type InferMutationInput<TRouteKey extends TMutation> =inferProcedureInput<AppRouter['_def']['mutations'][TRouteKey]>;/*** This is a helper method to infer the output of a subscription resolver* @example type HelloOutput = InferSubscriptionOutput<'hello'>*/export type InferSubscriptionOutput<TRouteKey extends TSubscription> =inferProcedureOutput<AppRouter['_def']['subscriptions'][TRouteKey]>;/*** This is a helper method to infer the asynchronous output of a subscription resolver* @example type HelloAsyncOutput = InferAsyncSubscriptionOutput<'hello'>*/export type InferAsyncSubscriptionOutput<TRouteKey extends TSubscription> =inferSubscriptionOutput<AppRouter, TRouteKey>;/*** This is a helper method to infer the input of a subscription resolver* @example type HelloInput = InferSubscriptionInput<'hello'>*/export type InferSubscriptionInput<TRouteKey extends TSubscription> =inferProcedureInput<AppRouter['_def']['subscriptions'][TRouteKey]>;
utils/trpc.tstsx// trpc-helper.ts// Import AppRouter from your main server routerimport type {inferProcedureInput,inferProcedureOutput,inferSubscriptionOutput,} from '@trpc/server';import type { AppRouter } from 'api/src/routers/_app';/*** Enum containing all api query paths*/export type TQuery = keyof AppRouter['_def']['queries'];/*** Enum containing all api mutation paths*/export type TMutation = keyof AppRouter['_def']['mutations'];/*** Enum containing all api subscription paths*/export type TSubscription = keyof AppRouter['_def']['subscriptions'];/*** This is a helper method to infer the output of a query resolver* @example type HelloOutput = InferQueryOutput<'hello'>*/export type InferQueryOutput<TRouteKey extends TQuery> = inferProcedureOutput<AppRouter['_def']['queries'][TRouteKey]>;/*** This is a helper method to infer the input of a query resolver* @example type HelloInput = InferQueryInput<'hello'>*/export type InferQueryInput<TRouteKey extends TQuery> = inferProcedureInput<AppRouter['_def']['queries'][TRouteKey]>;/*** This is a helper method to infer the output of a mutation resolver* @example type HelloOutput = InferMutationOutput<'hello'>*/export type InferMutationOutput<TRouteKey extends TMutation> =inferProcedureOutput<AppRouter['_def']['mutations'][TRouteKey]>;/*** This is a helper method to infer the input of a mutation resolver* @example type HelloInput = InferMutationInput<'hello'>*/export type InferMutationInput<TRouteKey extends TMutation> =inferProcedureInput<AppRouter['_def']['mutations'][TRouteKey]>;/*** This is a helper method to infer the output of a subscription resolver* @example type HelloOutput = InferSubscriptionOutput<'hello'>*/export type InferSubscriptionOutput<TRouteKey extends TSubscription> =inferProcedureOutput<AppRouter['_def']['subscriptions'][TRouteKey]>;/*** This is a helper method to infer the asynchronous output of a subscription resolver* @example type HelloAsyncOutput = InferAsyncSubscriptionOutput<'hello'>*/export type InferAsyncSubscriptionOutput<TRouteKey extends TSubscription> =inferSubscriptionOutput<AppRouter, TRouteKey>;/*** This is a helper method to infer the input of a subscription resolver* @example type HelloInput = InferSubscriptionInput<'hello'>*/export type InferSubscriptionInput<TRouteKey extends TSubscription> =inferProcedureInput<AppRouter['_def']['subscriptions'][TRouteKey]>;