Create a function with spread arguments to interop with js

Hello there again! Hope I’m not being tedious for creating posts that much.

I’m struggling with creation of a function with spread arguments.

Lets say I need to interop with the function:

function onSomeEventOccurred(handler: (string, ...number[]) => void): void {
  	// Actual implementation is hidden
  	setInterval(() => {
		handler("event-occurred", ...[1, 2, 3]);  
  	}, 1000);
}

In ts, I’d use the function like so:

onSomeEventOccurred((channel, ...numbers) => {
	console.log(channel, numbers);  
})

In ReScript I’ve came up with the following solution:

// How to write a type for that handler?
@val
external onSomeEventOccurred: 'a => unit = "onSomeEventOccurred"

@val
external arguments: 'a = "arguments"

onSomeEventOccurred(() => {
  let args = arguments
  let arg0: string = args[0]
  let rest: array<float> = args->Belt.Array.slice(~offset=1, ~len=Belt.Array.length(args))
  Js.log2(arg0, rest)
})

Sure, I can make a wrapper for onSomeEventOccured like so:

let onSomeEventOccurredWrapper: ((string, array<float>) => unit) => unit = handler => {
  onSomeEventOccurred(() => {
    let args = arguments
    let arg0: string = args[0]
    let rest: array<float> = args->Belt.Array.slice(~offset=1, ~len=Belt.Array.length(args))
    handler(arg0, rest)
  })
}

onSomeEventOccurredWrapper((channel, numbers) => {
  Js.log2(channel, numbers)
})

Playground Link

But it still uses arguments. Is there a better way to create a function with spread arguments without creation of a wrapper and usage of arguments?

Actual real world example would be Electron.ipcMain.on listener

Use Bind to JS Function | ReScript Language Manual

I don’t need to to grab an existing function from some package. I need to create a function which can get variable count of arguments.

upd:

An example of such function written in ts:
(arg1: string, ...rest: number[]) => console.log(arg1, rest)

I need to create similar function in rescript

Oh, OK. ReScript doesn’t support that.

Ok, seems like the only way is to use binding to arguments. I just thought that if there’s a @variadic attribute, there’ll be away to get rest arguments.