Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1770

Accessing and tweaking Dom elements (maybe related to type conversion?)

$
0
0

It’s safe to use with dicts, where all the keys will gave the same type. Otherwise, not so much, no. You can also constrain the allowed property names by using polymorphic variants, and have multiple differently typed functions that way:

@get_index external getStrProp : ('a, [#foo | #bar]) => option<string> = ""
@get_index external getFloatProp : ('a, [#x | #y]) => option<float> = ""

Yes, if you get the boundaries typed correctly, the compiler will help you avoid errors that can otherwise be very hard to track down. That principle doesn’t just apply to JS interop, but also the boundary between client and server, making sure the data you receive from the server is actually what you expect.

For methods you should almost always use @send. A record with function types can cause problems binding to this.

There are some ready-made bindings in rescript-webapi. But since it’s a big API that isn’t very well-suited for static typing, rescript has evolved quite a bit since this was initially designed, and direct DOM manipulation is quite rare in rescript, it’s usually better to just bind to it yourself. You can copy and paste from these bindings to get something up quickly though, and then just adapt to your needs.


Viewing all articles
Browse latest Browse all 1770

Trending Articles