tRPCttRPC
Powered by
AndrewA
tRPC•2y ago•
1 reply
Andrew

Procedure passed as argument to function loses type safety.

Hi there, I have a mystifying TS issue, where I lose type safety of my proc when I pass it as an argument to another function. I'm new to tRPC, and been doing a spike to see if we can adopt it at our company.

Full code example is in this TS playground: https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAb2AO2DAKgJQAoGEA0cWeAolFNIQILICe2FAxgKYAmArlMwL5wBmFEHABEAARhQwjAPQBnZlABuC4QChQkWHABUcAIay4ALwit+gkSdbCA3KtWMIyWfHgBeOCjTFcAOkfIMMwAHjAAPAhw7PJQAFyIcBQANsyy8S5QKADmANoAunC8AD5RyKzMfChshQB8ABQAlP5cekGNdqohmvABLokQ7EFQcB4wvhSDCnZd0D1OfWDsAEZJwIwMECwcXCNwY2BMbJzMHTC0YMxwG4y7mMwwnMjo58xhZxcQfHCLK2vXR1xfChFjAah0Zlpej0WkEfCQypAUO44GF0HAQkEyoZrvUDpt4uhCAB9CBgGDAebxBDcBojGqIVRwRL3R7fJi+aLMOp1UkwWS0tz03myXzIDF1SKMGDBKn8CAQeIAcj48sVhRpDVU3HBwW6cChcFWfQ8jBhzDhCIgSLqjO+y1W60O22YQOQILqVl8ECWACtmFKJYbQGh4p7kOwQEsFI0vWSKcg9ElGoUGg18LbqaomgBHdgKWjc4UC+kIW3SaTo3X++CrEBoPYQOBRvYvMyOcjVmx7AAWwEMsm7AySZmbemQ6PI0Cb-r0nKDdfgfbgAANw5GFHASuwyhUqqxl2WKygXMw9KxCGhFYZGJwuIEkrQV2PaAemeW4OJZABaLrVn+TqBbQNIlZAARnSCRsl2YVXRBAB+XxazQOwmWA2QACYIMyZAsmgskRSlYJfBVCAUOZB4oHHfItQaOwgA

The issue is that with the definition of
createTRPCEndpoint
createTRPCEndpoint
, the returned procedure loses the type definition of
input
input
.

I've simplified it to the point that
createTRPCEndpoint
createTRPCEndpoint
has no utility, but we use it to dynamically create middleware that are configured by the options we pass in. It would be important to be able to enrich the procedure
ctx
ctx
type with the middleware. If we didn't, we could override and cast the return type of
createTRPCEndpoint
createTRPCEndpoint
as
T
T
, and then the type safety of
input
input
is preserved, but then we can't enrich
ctx
ctx
.

Has anyone had success with passing procedures to other functions without losing their type safety? I'm wondering if there's some typing helpers that I'm missing, or other creative ways of solving this problem.

Thanks for your time! 🙏

Env: Node 18, yarn 4.0.2, npm 10.5.0.
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

type safety with meta
BeanBBean / ❓-help
2y ago
Type safety with enabled option
alexAalex / ❓-help
4y ago
Argument of Type not assignable
automataAautomata / ❓-help
3y ago
Get specific type of mutation as function arg
Answer OverflowAAnswer Overflow / ❓-help
2y ago