elm-ts-json
elm-ts-json copied to clipboard
Add tests to document behavior of `Codec.nullableField` and `Code.maybeField`
Thank you @dillonkearns for creating elm-ts-interop and also this nice package.
I created this PR to document a behavior that was unexpected to me:
I had expected that nullableField
returns an Err
when the field exists but the decoder fails. But it returns an Ok Nothing
.
The generated TS type seems like it should not hide the error message { f : number | null }
when passing { f: "string"}
.
I'm not sure if this is the intended behavior or a bug, so I first wanted to add the tests and then ask for your opinion.
Would you be open to an addition of a codec with this documentation? Or changing the behavior of nullableField
?
- If the value is
null
the decoder will returnNothing
. - If the value has the wrong type, the encoder will error out.
- If the value is
Nothing
then the resulting object will contain the field with anull
value.
It seems related to https://github.com/miniBill/elm-codec/pull/9#issuecomment-852930090 and it might also very well be the case that you are not interested in changing the behavior because it breaks backwards compatibility or because you want to stay close to minibills codec implementation.