Typed wrappers for procedures

Hhagabaka4/10/2023
I have a TRPC client with working queries and mutations. I wanted to create wrapper functions for all the procedure so that instead of trpc.someQuery.query(...) I can use someQuery(...), and instead of trpc.someMutation.mutate(...), someMutation(...). I'm having trouble with getting the wrapped functions to have correct parameter and return types. Currently I have:
function wrap<F extends ((...args: any[]) => any)>(fnWithClient: (c: typeof client) => F): (...args: Parameters<F>) => ReturnType<F> {
  return (...args: Parameters<F>) => fnWithClient(getClient())(...args);
}
export const someQuery = wrap((client) => client.someQuery.query)
export const someMutation = wrap((client) => client.someMutation.mutation)

It works in run time, and even shows correct types in VSCode, but when I generate a .d.ts, it says these functions have type (input?: unknown, options?: ProcedureOptions | undefined) => Promise<unknown>. How can I fix this issue, and is there a more elegant way to create such wrapper functions? I would prefer to use something like export const someQuery = wrap('someQuery').
Ttrash_dev4/10/2023
i wonder if this is what happens when you try to leverage types that are internal to the package, i have tried myself though tbh

@julius does that sound right? just guessing
UUUnknown User4/10/2023
Message Not Public
Sign In & Join Server To View
Hhagabaka4/10/2023
Would AnyQueryProcedure include the type of trpc.someQuery?
Hhagabaka4/10/2023
I don't understand DecorateQuery used in that function. Seems to be this gigantic type defined here https://github.com/trpc/trpc/blob/19c7c27d8ed548b2bc7ab5eb921fd179143aa230/packages/react-query/src/createTRPCReact.tsx#L87
UUUnknown User4/10/2023
2 Messages Not Public
Sign In & Join Server To View
Hhagabaka4/10/2023
When I try to assign const a: AnyQueryProcedure = client.someQuery, it says it is missing properties _def, _type, and _procedure
Hhagabaka4/10/2023
I'm justing createTRPCProxyClient, not the with react
UUUnknown User4/10/2023
3 Messages Not Public
Sign In & Join Server To View
Hhagabaka4/10/2023
I have a mini library that used to have only functions that make fetch calls, and now I'm adding an TRPC API. I want to keep the exports consistent so prefer to export functions for the individual procedures instead of a TRPC client. Also I want the client to be lazily created when any of the procedures is used.
Nnlucas4/10/2023
Isn't this basically it?

function makeWrap<T>(t: T) {
  return function wrap<Q>(func: (t: T) => Q) {
    return func(t)
  }
}

const wrap = makeWrap(trpcClient)
const someId = 1
wrap(c => c.post.byId.query(someId))
Nnlucas4/10/2023
Not exactly sure what's to be gained by using tRPC types in the implementation here, it would be useful to see a more complete example so we can advise