tRPCttRPC
Powered by
tendies123T
tRPC•3y ago•
1 reply
tendies123

Get Name of All Query Routes or Mutation Routes

Typescript is pretty unhappy with my methods of doing this manually, so wondering if there are any official ways
although they i'm pretty sure they should work objectively i.e.

export type MutationRoutes = {
  [K in RouteNames]: TRPCProxyClientType[K] extends {
    mutate: (...args: any[]) => any;
  }
    ? K
    : never;
}[RouteNames];
export type MutationRoutes = {
  [K in RouteNames]: TRPCProxyClientType[K] extends {
    mutate: (...args: any[]) => any;
  }
    ? K
    : never;
}[RouteNames];

If I use this
// This expression is not callable.
const manageTRPCMutationForXState = async <T extends MutationRoutes>(name: T) => {
const mutator = trpcProxyClient[name];
mutator.mutate()
...
}
// This expression is not callable.
const manageTRPCMutationForXState = async <T extends MutationRoutes>(name: T) => {
const mutator = trpcProxyClient[name];
mutator.mutate()
...
}


But if I do
const mutator = trpcProxyClient[name as MutationRoutes];
const mutator = trpcProxyClient[name as MutationRoutes];

It works
https://trpc.io/docs/client/vanilla/infer-types
Inferring Types | tRPC
It is often useful to access the types of your API within your clients. For this purpose, you are able to infer the types contained in your AppRouter.
Inferring Types | tRPC
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Compressing parts of query/mutation input
Samuel BarnholdtSSamuel Barnholdt / ❓-help
3y ago
Best way to get the type of the first argument for .query or .mutation
DavidDDavid / ❓-help
2y ago