From aeb2e1b8e33c4b8ced132c14489d1159e3298969 Mon Sep 17 00:00:00 2001 From: Robert Soriano Date: Fri, 20 May 2022 11:42:40 -0700 Subject: [PATCH] docs: add inference helpers recipe --- recipes/inference-helpers.md | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 recipes/inference-helpers.md diff --git a/recipes/inference-helpers.md b/recipes/inference-helpers.md new file mode 100644 index 0000000..78bf4d4 --- /dev/null +++ b/recipes/inference-helpers.md @@ -0,0 +1,80 @@ +## Inference Helpers + +`@trpc/server` exports the following helper types to assist with inferring these types from the `router` exported in `~/server/trpc/index.ts`: + +- `inferProcedureOutput` +- `inferProcedureInput` +- `inferSubscriptionOutput` + +```ts +// ~/utils/trpc.ts +import type { router } from '~/server/trpc/index.ts' + +type AppRouter = typeof router + +/** + * 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 = 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 = 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 = + inferProcedureOutput + +/** + * This is a helper method to infer the input of a mutation resolver + * @example type HelloInput = InferMutationInput<'hello'> + */ +export type InferMutationInput = + inferProcedureInput + +/** + * This is a helper method to infer the output of a subscription resolver + * @example type HelloOutput = InferSubscriptionOutput<'hello'> + */ +export type InferSubscriptionOutput = + inferProcedureOutput + +/** + * This is a helper method to infer the asynchronous output of a subscription resolver + * @example type HelloAsyncOutput = InferAsyncSubscriptionOutput<'hello'> + */ +export type InferAsyncSubscriptionOutput = + inferSubscriptionOutput + +/** + * This is a helper method to infer the input of a subscription resolver + * @example type HelloInput = InferSubscriptionInput<'hello'> + */ +export type InferSubscriptionInput = + inferProcedureInput +```