Closed cannorin closed 3 years ago
Note that the test in #125 should be updated to reflect the addition of boolean union discriminators.
Also there are many redundant match cases in the current implementation like this: https://github.com/LexiFi/gen_js_api/blob/28b43a33037a29cbe6e91f07600f4f432d73c778/ppx-test/expected/union_and_enum.ml#L288-L291
I think I can do another PR to eliminate these kind of dead match cases (and it will solve the above problem too). What do you think?
Thanks for the contribution, which makes perfect sense. I've pushed some commits to your branch, to simplify a bit the code around the changes in this PR (even if not strictly related to it). Do you agree with those changes?
Yes, thanks 🙂
TypeScript allows boolean values to be used as union discriminators since it allows Boolean Literal Types. It's better to support boolean union discriminators in gen_js_api too.
Note there is a real-world example of such union types: https://github.com/microsoft/TypeScript/blob/master/lib/typescript.d.ts#L5857-L5874
Also, TS allows an union of boolean values and other literal types for the same reason:
Unions of literal types like above should easily be mapped to OCaml with gen_js_api's
[@js.enum]
. Although JS and TS do not have "boolean enums", it is convenient to just pretend as if they do and add boolean support to[@js.enum]
.With this PR merged, the union type shown above can be mapped as:
and should generate the following:
TODO: