-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More idiomatic optional properties on runtime InterfaceCombinator output #26
Comments
mmkal
added a commit
to mmkal/io-ts-codegen
that referenced
this issue
Aug 31, 2018
For interfaces with optional properties, use either partialCombinator (for all optional) or intersectionCombinator (for mixed) to allow for true optional properties, which don't require explicitly assigning undefined. Addresses gcanti#26
gcanti
pushed a commit
that referenced
this issue
Sep 3, 2018
For interfaces with optional properties, use either partialCombinator (for all optional) or intersectionCombinator (for mixed) to allow for true optional properties, which don't require explicitly assigning undefined. Addresses #26
Closing via #27 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like from gcanti/io-ts#140 that it's non-trivial to declare a key as optional based on properties of the value, although I still hope that there'll be some sort of TypeScript 2.8 magic that makes it possible. But given that io-ts-codegen is generating types as a string, it should be possible to do better than this:
by using the
isOptional
flag on thePropertyCombinator
. ChangingprintRuntimeInterfaceCombinator
to something like this would maybe do the trick:then you get fairly sensible output, at least until optional values can become more natural in io-ts itself:
Advantages being 1) that the optional properties are truly optional, and don't have to be explicitly assigned to undefined, and 2) it's more readable than seeing
t.union([t.blah, t.undefined])
everywhere.If you like the idea, feel free to just take the code straight from here. Or I could open a PR with something along these lines (and add a couple of tests).
The text was updated successfully, but these errors were encountered: