How to make optional fields with default values be inferred as required in the TypeScript type? #1517
Unanswered
GustavoHenriqueP
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.
-
Hello! Thanks for the lib.
I'm using it with Fastify for schema validation and really enjoying it so far. But I hit a roadblock and would like some help, if possible.
A behavior that would really come in handy would be the possibility to make a schema like this:
To generate a type like this when using
Static<T>:Instead of this:
I know that to apply the defaults to the value in runtime I can use
Value.Default, which returnsunknown, but I can also useValue.Parseto apply defaults AND return the TS type with all fields being optional in this case.But there is a way to apply defaults AND return the TS type with only the fields with
Type.Optionaland without a defined default value being really optional?I'm aware that it might cause inconsistent behavior if I use this value without checking it before. But my plan was to make this parse after the parsing and validation step of Fastify's request lifecycle, which turns to be safe. This would allow to:
Beta Was this translation helpful? Give feedback.
All reactions