it's not that different compared to using the FFI.
The link below shows how it can work for Ocaml <-> TypeScript.
https://github.com/ocsigen/ts2ocaml