tRPCttRPC
Powered by
neoneyN
tRPC•3y ago•
1 reply
neoney

Access procedures with same names and same parameters, but in different routers, generically

My trpc structure looks something like this:

trpc
  - router1
    - routerX
      - proc1
  - router2
    - routerX
      - proc1
trpc
  - router1
    - routerX
      - proc1
  - router2
    - routerX
      - proc1


Basically, I have multiple routers, but they all have pretty much the same signatures - they always have mostly the same parameters, but the return types are different for some. I can't currently do anything like
trpc[router].routerX.proc1.useQuery()
trpc[router].routerX.proc1.useQuery()
, because Typescript throws an error -
Each member of the union type [...] has signatures, but none of these signatures are compatible with each other.
Each member of the union type [...] has signatures, but none of these signatures are compatible with each other.


I can kinda work around it like this:

    type Params =
      | Parameters<typeof trpc.router1.routerX.proc1.useQuery>
      | Parameters<typeof trpc.router2.routerX.proc1.useQuery>;

    type Returned =
      | ReturnType<typeof trpc.router1.routerX.proc1.useQuery>
      | ReturnType<typeof trpc.router2.routerX.proc1.useQuery>;

    type Fn = (...args: Params) => Returned;

    return trpc[router].routerX.proc1.useQuery as Fn;
    type Params =
      | Parameters<typeof trpc.router1.routerX.proc1.useQuery>
      | Parameters<typeof trpc.router2.routerX.proc1.useQuery>;

    type Returned =
      | ReturnType<typeof trpc.router1.routerX.proc1.useQuery>
      | ReturnType<typeof trpc.router2.routerX.proc1.useQuery>;

    type Fn = (...args: Params) => Returned;

    return trpc[router].routerX.proc1.useQuery as Fn;


But in the returned useQuery hook, the type of
data
data
is unknown.
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

procedures on routers are typed any
allenwhunAallenwhun / ❓-help
10mo ago
different transformers for different routers
PinkiePiePPinkiePie / ❓-help
12mo ago
AppRouter nested routers/procedures are any when imported in monorepo
EilinisEEilinis / ❓-help
11mo ago
Procedures with paramters.
brumbrum_brumBbrumbrum_brum / ❓-help
2y ago