LexiFi / gen_js_api

Easy OCaml bindings for Javascript libraries
MIT License
177 stars 31 forks source link

Support ad-hoc object types to map to JS objects #157

Open Lupus opened 3 years ago

Lupus commented 3 years ago

Typescript definitions often define ad-hoc object types for parameters or return values, like this for example:

class TimeUuid extends Uuid {
    /* ... */
    getDatePrecision(): { date: Date, ticks: number };
  }

Would be awesome to be able to just write in .mli:

  (* class 'TimeUuid' *)
  module Time_uuid : sig
    class t :
      Ojs.t
      -> object
           inherit Uuid.t

           (* method 'getDatePrecision' *)
           (* signature: (): { date: Date; ticks: number; } *)
           method get_date_precision : unit -> < date : Date.t; ticks : int > [@@js.call "getDatePrecision"]
         end

    val js_of_t : t -> Ojs.t [@@js.cast]

    val t_of_js : Ojs.t -> t [@@js.cast]
  end

One could define a record type for this in advance, but this type would be somewhat artificial given that it's anonymous in "upstream" typescript typings and is not ever reused anywhere.