Abraham P
Abraham P

Reputation: 15471

ReasonML javascript record field

Say I have the following Bucklescript types:

type amqp;

[@bs.val] external amqpLib: amqp = "Amqp";

[@bs.module] external amqplib : amqp = "";

class type amqpConnectionT =
  [@bs]
  {
    pub createChannel: unit => unit;
    pub close: unit => unit
  };

type amqpConnection = Js.t(amqpConnectionT);

let make = () => amqplib;

[@bs.send] external connect : (amqp, string) => Js.Promise.t(amqpConnection) = "";

let connectAmqp = (input: string, amqpClient: amqp) : Js.Promise.t(amqpConnection) => connect(amqpClient, input);

let makeConnection = (input) => make() |> connectAmqp(input, _);

and then the following code:

let start = () =>
  Amqp.makeConnection("amqp://localhost")
  |> Js.Promise.then_(connection => {
    Js.log(connection);
    connection.createChannel();
    Js.Promise.resolve(connection);
  });

start();

This fails with:

The record field createChannel can't be found.

Why? How is my type annotation incorrect?

And what is the correct way to expose member methods like this in ReasonML?

Upvotes: 0

Views: 348

Answers (1)

Boyswan
Boyswan

Reputation: 224

try connection##createChannel() if it's coming from JS.

The compiler thinks that connection is a record as you're using the . accessor

Upvotes: 2

Related Questions