Inferring Types

It is often useful to wrap functionality of your @trpc/client or @trpc/react api within other functions. For this purpose, it’s necessary to be able to infer input types, output types, and api paths generated by your @trpc/server router.

Inference Helpers

@trpc/server exports the following helper types to assist with inferring these types from the AppRouter exported by your @trpc/server router:

  • inferProcedureOutput<TProcedure>
  • inferProcedureInput<TProcedure>
  • inferSubscriptionOutput<TRouter, TPath>

Example Code

utils/trpc.ts

  1. // trpc-helper.ts
  2. import type { AppRouter } from 'api/src/routers/_app' // Import AppRouter from your main server router
  3. import type { inferProcedureOutput, inferProcedureInput, inferSubscriptionOutput } from '@trpc/server'
  4. /**
  5. * Enum containing all api query paths
  6. */
  7. export type TQuery = keyof AppRouter['_def']['queries']
  8. /**
  9. * Enum containing all api mutation paths
  10. */
  11. export type TMutation = keyof AppRouter['_def']['mutations']
  12. /**
  13. * Enum containing all api subscription paths
  14. */
  15. export type TSubscription = keyof AppRouter['_def']['subscriptions']
  16. /**
  17. * This is a helper method to infer the output of a query resolver
  18. * @example type HelloOutput = InferQueryOutput<'hello'>
  19. */
  20. export type InferQueryOutput<TRouteKey extends TQuery> = inferProcedureOutput<
  21. AppRouter['_def']['queries'][TRouteKey]
  22. >
  23. /**
  24. * This is a helper method to infer the input of a query resolver
  25. * @example type HelloInput = InferQueryInput<'hello'>
  26. */
  27. export type InferQueryInput<TRouteKey extends TQuery> = inferProcedureInput<
  28. AppRouter['_def']['queries'][TRouteKey]
  29. >
  30. /**
  31. * This is a helper method to infer the output of a mutation resolver
  32. * @example type HelloOutput = InferMutationOutput<'hello'>
  33. */
  34. export type InferMutationOutput<TRouteKey extends TMutation> =
  35. inferProcedureOutput<AppRouter['_def']['mutations'][TRouteKey]>
  36. /**
  37. * This is a helper method to infer the input of a mutation resolver
  38. * @example type HelloInput = InferMutationInput<'hello'>
  39. */
  40. export type InferMutationInput<TRouteKey extends TMutation> =
  41. inferProcedureInput<AppRouter['_def']['mutations'][TRouteKey]>
  42. /**
  43. * This is a helper method to infer the output of a subscription resolver
  44. * @example type HelloOutput = InferSubscriptionOutput<'hello'>
  45. */
  46. export type InferSubscriptionOutput<TRouteKey extends TSubscription> =
  47. inferProcedureOutput<AppRouter['_def']['subscriptions'][TRouteKey]>
  48. /**
  49. * This is a helper method to infer the asynchronous output of a subscription resolver
  50. * @example type HelloAsyncOutput = InferAsyncSubscriptionOutput<'hello'>
  51. */
  52. export type InferAsyncSubscriptionOutput<TRouteKey extends TSubscription> =
  53. inferSubscriptionOutput<AppRouter, TRouteKey>
  54. /**
  55. * This is a helper method to infer the input of a subscription resolver
  56. * @example type HelloInput = InferSubscriptionInput<'hello'>
  57. */
  58. export type InferSubscriptionInput<TRouteKey extends TSubscription> =
  59. inferProcedureInput<AppRouter['_def']['subscriptions'][TRouteKey]>