Open rhofour opened 4 years ago
Hi @rhofour ,
JsonDecode.string.decode('hello')
, if you try to JSON.parse('hello')
you'll get an exception. So, this is not an option.export class Decoder<a, b> {
constructor(private decodeFn: (json: b) => Result<a>) {}
decode(json: b): Result<a> {
Improving the API to avoid any
adds verbosity, complicates things quite a bit in some scenarios, and it would be a breaking change.
I think I'll stick to the "improve error messages" plan. I have some ideas.
I just spent a fair bit of time trying to debug why an object decoder was failing, only to eventually consult the code and realize it's because the input I was passing was a JSON string rather than an actual object.
I think there are a couple of possible ways this could be better: 1) The error given could clearly state the input is not an object rather than just saying it's not a valid decoderName. 2) Strings could be automatically parsed to json. 3) The input to the decoder could be set to a more specific type than Any so the compiler could catch this.
Let me know what your thoughts are. If you'd like I can send a PR to do some or all of these, but I want to hear what you think first.