ocsigen / ts2ocaml

Generate OCaml bindings from TypeScript definitions via the TypeScript compiler API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generate types inside of the module

tmattio opened this issue · comments

ts2ocaml generates types with an Internal.Types module.

From what I understand, this is to solve recursive modules definition and definition ordering. However, it makes the generated API not so user-friendly, and I have been making the types abstract for all of the generated code to improve the API.

IMHO, I'd prefer having abstract types and spending some time fixing occasional compilation errors.

Maybe this could be provided as an option?

This and #14 are mutually exclusive features.

Types in TS can be mutually recursive and also can extend each other, so to achieve #14 the modules have to be defined recursively. You can't have recursive modules without ruining the API i.e. diverting module types to somewhere like Internal.Modules, which is against this issue.

This and #14 are mutually exclusive features.

Indeed! In practice, the great majority of the ts definitions I have been using compiles fine when moving the types in the modules, but I understand that this feature breaks the guaranty that the generated code will compile.

That's why I mentioned that was fine with spending some time fixing occasional compilation error, and that the feature could be provided as an option e.g. --unsafe-inline-types.

FWIW, the only cases where I had to deal with mutually recursive modules, the functions were not that useful and I just worked around it by not binding them.

I just opened a PR to gen_js_api to support recursive modules as input: LexiFi/gen_js_api#153

This would allow us to safely inline all the types to their module, eliminating the Internal module completely.

Now we can generate types inside of the module with the --rec-module option from 340a53b .

  • --rec-module=optimized (default) applies strongly-connected-component algorithm to find the smallest sets of recursively defined types and modules
  • --rec-module=naive simply makes every module recursive. Not very recommended because hundreds of recursive modules would torture OCaml compiler.
  • --rec-module=off generates type outside of the module as before (except we get rid of Internal module and simply put types to the toplevel)