reasonrescript

How can I type a JS module method from Reason?


In an effort to refactor an existing JS based WebUI into a ReasonML, I'm trying to embed a some refactored code. Currently I'm doing this by embeddeding all the ReasonML code (so far) inside an iframe.

Space is extremely limited, so rather than adding a reasonml module that does the same, I would like to use one of the JS methods in my ReasonML code, but I'm not clear in on the syntax to type the call. In JS, the call is simply:

toastr.success("You're awesome");

There doesn't appear to be an explicit constructor for the toastr object, but I tried to do this regardless:

type bread;
[@bs.new]  external toastr: unit => bread = "toastr";
[@bs.send] external success: bread => string = "success"; 

and later in the code:

let bread = toastr();
bread->success("Would be surprised if this worked.");

The above doesn't compile, failing on the bread->success("") line with the message:

This expression has type string
It is not a function.

How do make this work (in bs 8.4.2)?


EDIT #2

I've gotten even closer to the JS output I'm hoping will work with:

[@bs.scope "toastr"][@bs.val] external success: string => unit = "success";

which I can invoke:

success("It worked?");

which produces the JS:

toastr.success("It worked?");

but it's throwing an exception at run time because it can't find the toastr module.

I think I'm on to a different problem now?


Solution

  • In the end, this is what I did:

    [@bs.scope "toastr"][@bs.val] external t_success:   string => unit = "success";