Convert to JS - handling `Option`

interop

#1

Hi guys, quick question regarding the convert to JS generators.
It seems that they do not have any special handling for Option types.
When an Option is None, the compiler mark it as 0. With no difference to a real value that might be 0.
My expectations were that None will be translated to Null or Undefined, but even without those, just marking it as 0 seems weird.
Here is an example:

Is there a way for the generated JS converters to handle “undefined” in some way?


#2

@snird Bucklescript offers the Js.Nullable.t type to allow you to express the idea of null/undefined. https://bucklescript.github.io/bucklescript/api/Js.Nullable.html

Taking the example you posted, you can convert between option and nullable by using the function Js.Nullable.fromOption:


#3

Got it! thanks!
So the mechanism of the “ConvertJS” is that it will always simply map the record array into an object as is, and I need to consider the types within the record to pass it? Or there might be some other mechanism behind I’m not considering?


#4

That is correct. Though probably requires a slightly more nuanced answer. Note that the jsConverter is actually not necessary in your example.


#5

And one last question, since I’m looking for a mechanism to specifically give me undefined.
the Js.Null_Undefined module says that “fromOption” will give back undefined in case of None:
https://bucklescript.github.io/bucklescript/api/Js.Null_undefined.html#VALfromOption

But in this example I still see Null:


#6

Js.Nullable.undefined will return undefined.