ocsigen / ts2ocaml

Generate OCaml bindings from TypeScript definitions via the TypeScript compiler API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Function to instantiate a class with implicit constructor

ulugbekna opened this issue · comments

Hi, thanks for ts2ocaml, which is very easy and pleasurable to use! 🙂
If a class doesn’t have an explicit constructor() declared in the .d.ts file, ts2ocaml doesn’t generate a constructor, ie, in the generated EventEmitter module there’s no function to create an instance of EventEmitter.t. Am I missing something?

Example:

Given the following class:

/**
	 * An event emitter can be used to create and manage an {@link Event} for others
	 * to subscribe to. One emitter always owns one event.
	 *
	 * Use this class if you want to provide event from within your extension, for instance
	 * inside a {@link TextDocumentContentProvider} or when providing
	 * API to other extensions.
	 */
	export class EventEmitter<T> {

		/**
		 * The event listeners can subscribe to.
		 */
		event: Event<T>;

		/**
		 * Notify all subscribers of the {@link EventEmitter.event event}. Failure
		 * of one or more listener will not fail this function call.
		 *
		 * @param data The event object.
		 */
		fire(data: T): void;

		/**
		 * Dispose this object and free resources.
		 */
		dispose(): void;
	}

generated module is

module EventEmitter : sig
    type 'T t = private Ojs.t
    type 'T t_1 = 'T t
    val t_to_js: ('T -> Ojs.t) -> 'T t -> Ojs.t
    val t_of_js: (Ojs.t -> 'T) -> Ojs.t -> 'T t
    val t_1_to_js: ('T -> Ojs.t) -> 'T t_1 -> Ojs.t
    val t_1_of_js: (Ojs.t -> 'T) -> Ojs.t -> 'T t_1
    
    (** The event listeners can subscribe to. *)
    val get_event: 'T t -> 'T Event.t [@@js.get "event"]
    
    (** The event listeners can subscribe to. *)
    val set_event: 'T t -> 'T Event.t -> unit [@@js.set "event"]
    
    (**
      Notify all subscribers of the 
      \{\@link 
      EventEmitter.event
      event
      \}
      . Failure
      of one or more listener will not fail this function call.
      @param data The event object.
    *)
    val fire: 'T t -> data:'T -> unit [@@js.call "fire"]
    
    (** Dispose this object and free resources. *)
    val dispose: 'T t -> unit [@@js.call "dispose"]
end

The command I ran:

ts2ocaml jsoo --create-minimal-stdlib --preset=minimal --rec-module=optimized --safe-arity=full --simplify=all vscode.d.ts

This is clearly my oversight. I can't believe why I failed to notice this...

Fixed in the latest commit. Please wait for the 1.4.0 release, which I hope is to be released tomorrow or in few days.

Meanwhile, you can build it locally (see here) if you want to try it out right now.

(I think I need to do a small tweak for how to move the anonymous interfaces)

thanks heaps!