Gh∅st
Gh∅st6mo ago

Create a typescript type that refers to a useQuery hook dynamically

I'm trying to create a typescript type that generically refers to a useQuery hook (e.g. I am trying to do type CommonHookFn = (name:string)=>MyCustomType, except I need the equivalent of a useQuery hook where I can templatize the inputs/outputs). From exploring the types under the hood of trpc, I think I need to use ProcedureUseQuery<TProcedure, TPath> to do this, but I can't figure out what I would put in to TProcesure/TPath to tell it that I'm looking for a Query with specific input/output types.. (e.g need a hook that takes a string as n input, and returns MyCustomType for its data) Does anyone know how to create a type like this? environment: Node 19, typescript 5.3.3, @trpc/client, @trpc/server, @trpc/next: 10.45.1 @tanstack/react-query: 4.36.1
Solution:
DEV Community
tRPC & React Patterns: Router Factories
This post comes in 2 halves: tRPC Router Factories Consuming Router Factories in a React...
Jump to solution
4 Replies
Nick
Nick6mo ago
Take a look at the polymorphism helpers
Solution
Nick
Nick6mo ago
DEV Community
tRPC & React Patterns: Router Factories
This post comes in 2 halves: tRPC Router Factories Consuming Router Factories in a React...
Nick
Nick6mo ago
Alternatively if you just want to pass a single function around you can easily take the typeof or reconstruct it from the inputs and outputs
Gh∅st
Gh∅st6mo ago
thanks! this is exactly what I'm trying to do!