-
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
Feature: 'nullToUndefined' decoder type #37
Comments
Thanks for the thorough explanation. What if instead of adding more deciders, we opened up the functionality of With that in mind, what do you think about having both an Basically we consolidate this
into this
If an API were to use omission and What do you think? |
Sounds like an optimal solution to me 🙂 |
@mulias To provide a touch of context for an API to leverage omission and I would argue that what was proposed should not be considered for default behavior; if the goal of the library is to perform type validation, it should attempt to be as precise in implementation as possible by default and the methods to do so should be the most discoverable. I won't say that providing a prefabricated method of coercing null to undefined should not be done, but it really should not override the behavior of |
Many APIs provide missing data fields with a value of
null
(see issue #29).As a consumer of such an API using TypeScript and this very library it is currently not possible to decode such values to
undefined
.Personally, I prefer to use
undefined
instead ofnull
for my TypeScript models and I try to avoid null wherever possible. While this might be just my own preference, I believe it would make sense to add a standard decoder to this library that will automatically transform a givennull
value toundefined
.Here is snippet that does exactly what I need:
const nullToUndefined = <T>(decoder: Decoder<T>): Decoder<T | undefined> => JTV.union(decoder, JTV.constant(null)).map((val) => val === null ? undefined : val);
I am not sure if this is the best solution to the given problem, but it works.
I am happy to file a PR if that is desired 🙂
The text was updated successfully, but these errors were encountered: