Idea: Adding a debug mode to the compiler (e.g. that adds runtime JavaScript type checks to JS bindings)


ReScript depends heavily on users creating bindings to JavaScript APIs. I’m wondering if it would be possible (and make sense) to add a debug mode, which checks at runtime if a binding is correct.

I believe, this could improve the developer experience of creating bindings because better error messages can be displayed (in the past I got some error messages in the browser that were quite cryptic to me, when binding to React components).

This probably also would lead to finding bugs earlier, that otherwise maybe would sneak into production.

One problem I see, is that only data that come from JavaScript to ReScript can be validated easily, not data that ReScript sends to JavaScript. But I think that would already be a big improvement.

For data that goes from ReScript to JavaScript maybe exceptions could be caught to print nicer error messages (at least, when binding to functions).

A debug mode would make the build process a bit more complicated, because now there would be two build types, but I believe it would be worth it.

What do you think?

we do have a debug mode for showing values nicely.
I had some ideas for data validation for bindings, the challenge is that such data validation could only be done in the best effort, no guarantee could be provided

Is this documented somewhere? I could find anything.

the challenge is that such data validation could only be done in the best effort, no guarantee could be provided

Yes, no guarantees could be given. But for example, if we have:

@val external foo: int => int = "foo"

ReScript could assert in debug mode that foo returned an int and if not, show a nice error message or warning. If the return value of foo is passed around, that could lead to runtime errors that are not obvious, I think.

ReScript also could check at runtime if foo really exists (at module level, so errors would be shown directly after startup).

There are probably more scenarios like these where the compiler would be able to generate better error messages or warnings.

These checks couldn’t guarantee that the bindings are correct and would be a little like lints, but I think some help or some improved error messages are better than none.