Search code examples
ocamlrecordreasonbucklescript

reasonml record vs JS object


Say I define the following types:

type queueParams = {
  durable: bool
};

class type amqpChannelT = [@bs] {
  pub assertQueue: string => queueParams => Js.Promise.t(unit);
};

Then calling the following:

channel##assertQueue("exampleQueue", {"durable": bool});

Results in:

This has type:
    {. "durable": bool}
  But somewhere wanted:
    queueParams (defined as

How can I pass the right type of thing? Why is the thing I'm passing not a record? And what is the meaning of that dot notation?


Solution

  • ReasonML interprets these two differently:

    let jsObject = {"durable": true};
    let reasonRecord = {durable: true};
    

    Basically, wrapping keys with double-quotes is a short-hand notation for the special Javascript object type Js.t('a) - which is currently deprecated.

    You can play around with an example here. Note how the two types are treated differently when converted to Javascript.

    Read more about the deprecated syntax here:

    https://bucklescript.github.io/docs/en/object-deprecated