How can I generate trpc Generic Types?
I am building a trpc library, and as part of my implementation, I need the generic types of
t
, t.mergeRouters
, t.procedure
, t.router
and t.middleware
.
I've haven't been able to locate those generic types in the @trpc/server
and since trpc has such a complex type structure, would love to see an implementation for a generic set of types for the ones listed about.2 Replies
It's best to find another way generally, you can't really get away from coupling your routers and middlewares to the
t
instance right now, and it's not clear if that could ever be possible while retaining the full benefits of typescript
Could you share a bit more about your use case?@Nick Lucas Yes, I am building an implementation for trpc in nestjs, a full opinionated one, for example here is how you'll define a route:
I have managed to get it all to work, but it is littered with
any
types since trpc doesn't really give us actual types, for example, if I want the Router()
decorator to be able to accept meta
, I don't really have a way to type it since its typed dynamically.
Here is an example of how I generate the routers:
as you can see, it is really difficult to actually use types here, and I am forced to use any
for most of the parts in this function, I would love to just pass t
and use it throughout the function but t
doesn't really lends itself with a generic type that I can actually use.
And yes, I know I will lose the compile time benefits of trpc, but as you can see by my answer here: https://github.com/macstr1k3r/trpc-nestjs-adapter/issues/1#issuecomment-1483129949
The best solution for integrating trpc with nestjs is taking the @nestjs/graphql
approach and generating the schema in build-time.GitHub
Brainstorm: decorators for routing procedures? · Issue #1 · macstr1...
I am wondering if there is a way to create custom decorators to route the procedures, i.e. @Controller('api/cats') export class CatsController { @Mutation({ input: z.object({ name: z.string...