Andrew
Andrew7mo ago

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, the returned procedure loses the type definition of input. I've simplified it to the point that 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 type with the middleware. If we didn't, we could override and cast the return type of createTRPCEndpoint as T, and then the type safety of input is preserved, but then we can't enrich 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.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?