Dynamically .merge or .extend
#1172
Unanswered
fehnomenal
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First of all thanks for this great library. I use it everywhere I can 👍
Lately I work with Rest APIs where the response shape depends on the request.
It is something like this
{ "searchQuery": "...", "searchType": 0, "includeGenericArticles": true }The response will contain generic article information only if
includeGenericArticleswas given astrue:{ "totalMatchingArticles": 1, "maxAllowedPage": 1, "articles": [ { "articleNumber": "...", "genericArticles": [ {}, {} ] } ], "status": 200 }At runtime I can express this easily by building the response schema dynamically:
This works and now I want to get type-safety also at compile time, i.e.:
It tried it with generics, conditional types and
z.extendShapebut it won't work:If I remove the ternary and use always the extension shape, it type checks.
Has anybody an idea how I can solve this problem?
Beta Was this translation helpful? Give feedback.
All reactions