Replies: 1 comment 2 replies
-
You can create a Zod union of the two response schemas and then create a check that eliminates one of the types. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
We have a micro service architecture inside a monorepo and all our services are using trpc. Some services can be called by other internal micro services as well as some external services.
If called from an external service we want to hide some fields. Here is a simplified code example to get the idea:
Our problem is that the type of the route is not conditional (in our case it's always the type for the external route with less attributes). As far as we know typescript does not support conditional types at the moment (see: #2888).
It seems that we have the following options:
1. Create two queries (one for internal and one for external)
This has the drawback that we need to duplicate code. Especially for complex queries this is something we want to avoid.
2. Typecast the correct type manually on one side
This has the drawback that we have to typecast every usage of the query (besides that a typecast is only a workaround)
3. Create two router types
This is a variation of 2. but instead of typecasting every query usage we could change the Router type for on side:
This has the advantage that we only need to fix the type once and the usage of the query stays the same as long as they import the correct type.
The type
CreateExternalRouter
is basically a DeepOmit type that omits the attributes we don't want to have inside the external router.Our problem here is that we get an error message:
It seems that our External Router type is not compatible with the trpc client.
So our questions are:
I tried to find a solution for this for several days now. Any help or suggestions are welcome :-)
Beta Was this translation helpful? Give feedback.
All reactions