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 @bs.as 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

@bs.obj
external number: (
  ~id: string=?,
  ~placeholder: string=?,
  ~style: string=?,
  ~type_: string=?,
  ~format_: string=?,
  unit,
) => number = ""

@bs.obj
external props: (~number: number=?, ~cvv: cvv=?, unit) => props = ""

@bs.new 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 = {
  @bs.optional
  id: string,
  @bs.optional
  placeholder: string,
  @bs.optional
  style: string,
  @bs.optional @bs.as("type")
  numberType: string,
  @bs.optional @bs.as("format")
  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

@bs.obj
external number: (
  ~format: string=?,
  ~_type: string=?,
  unit,
) => 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: