benmerckx / genes

Generates split ES6 modules and Typescript definitions from Haxe modules.
43 stars 8 forks source link

Support custom enum discriminator #70

Closed kevinresol closed 1 year ago

kevinresol commented 1 year ago

Emit a field under the defined name, with value being the enum constructor name. For nicer consumption in native js code (without relying on the ugly _hx_index field).

Example: -D genes.enum_discriminator=$kind

Would emit haxe.ds.Option.None as {_hx_index:0, $kind:"None"} and haxe.ds.Option.Some(42) as {_hx_index:1, $kind:"Some", value:42} (not exact but you get the idea)

So in native JS/TS code one can consume the value with:

switch (option.$kind) {
  case "None":
  case "Some": option.value // type-safe access
}
benmerckx commented 1 year ago

We'll want to add this to TS definitions as well