Skip to content

Infer type from zod / narrowing type down to string and undefined #265

Answered by hayes
BengtHagemeister asked this question in Q&A
Discussion options

You must be logged in to vote

Unfortunately, there currently isn't a good way to narrow the types. The validation plugin currently does not have the ability to change the types for a field. This basically comes down to there not being a way to inject an extra generic into the signature of the t.string method that could be used to infer the schema shape and to use that to overwrite other generics used by that method.

If this is an important feature, one way we could achieve this is the create a new builder method (t.validatedField({ type: 'String', validate: {...}}) or something similar), that has the ability to infer an extra generic, and narrow the type correctly.

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by BengtHagemeister
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants