I have a React component <Switch type="switch" /> (or type="checkbox") from JS that takes a type argument. Can I import it to ReScript with @module external make and make it more idiomatic by converting the argument to ~type_: [#switch_ | #checkbox]?
I tried the @as annotation in the arguments, but it doesn’t work well enough with @react.component. @string [@as("switch") #switch_ | #checkbox] works for \"type"=#switch_ and converts it to type: "switch", but in case of @as("type") ~type_:, type_=#checkbox still compiles to type_: "checkbox", which of course does not work with the JS component.
Thanks! I learned from the docs that @as("type") type_ can rename object fields. Is it also idiomatic to put the underscore before the original name? If so, I would use #_switch to make naming consistent in my bindings.
Is it also idiomatic to put the underscore before the original name?
I just suspect this is just a special syntax for ReScript React props that are reserved words.
For example, the same syntax applies to other reserved words such as ~_module, ~_let, ~_external and ~_while. Interestingly it doesn’t apply to ~_switch.
Outside of React component props I’ve typically seen identifier_ being used when the identifier is a reserved word and _identifier to ignore the identifier when it’s a function argument (similar to just using _ alone).
Someone with more knowledge might be able to explain further.