To model a JS function that takes a variable number of arguments, and all arguments are of the same type, use bs.variadic
:
[@bs.scope "Math"] [@bs.val] [@bs.variadic] external max: array(int) => int = "";
Js.log(max([|5, -2, 6, 1|]));
This compiles to:
console.log(Math.max(5, -2, 6, 1));
Similarly to Function.apply()
in JS, bs.variadic
converts the arguments array in BS to separate arguments on the JS side.
If your function has mandatory arguments, you could use bs.as
to add them:
[@bs.val] [@bs.variadic] external warn: ([@bs.as 404] _, [@bs.as "NOT_FOUND"] _, array(string)) => unit = "log";
warn([||]);
warn([|"this", "page", "is", "not", "found"|]);
This compiles to:
log(404, "NOT_FOUND");
log(404, "NOT_FOUND", "this", "page", "is", "not", "found");