Mapping reserved props names when using named props

I am looking to make the name of a prop to a binding map without the underscore.

I have used the decorator in the past, but this was for a record not a named argument to a function. How would I map ~format_ to format when its passed to the binding?

type number

external number: (
  ~id: string=?,
  ~placeholder: string=?,
  ~style: string=?,
  ~type_: string=?,
  ~format_: string=?,
) => number = ""

external props: (~number: number=?, ~cvv: cvv=?, unit) => props = "" external create: (string, props) => fattJs = "FattJs"

Thanks in advance!

There is no way to rename fields with bs.obj. However bs.deriving in abstract form will do almost the exact same thing bs.obj does, and is defined with record syntax.

@bs.deriving({abstract: light})
type number = {
  id: string,
  placeholder: string,
  style: string,
  numberType: string,
  numberFormat: string,

Js.log(number(~numberType="int", ()))

Why do you add a _ to format? Is it a keyword? You probably want to special case type properly, like this:

type number

external number: (
  ~format: string=?,
  ~_type: string=?,
) => number = ""

let a = number(~format="test", ~_type="test", ())

which transforms to following JS:

var a = {
  format: "test",
  type: "test"

exports.a = a;

Playground Link here

Preceding _ are stripped from the labeled argument in this case.

1 Like

Ah! I thought I saw somewhere that the underscores were escaped, but I guess I missed that it needs to be a prefix :+1:t2:

As for format, I was not 100% sure if it was reserved as vim did some highlighting haha :joy: