Rest arguments for callback

There is a js function

export function callback(event: string, listener: (...args: any[]) => void): void;

I tried to wrap this in an extern class

function callback(event:String, listener:Rest<Dynamic>->Void):Void;

But when you try to call it

callback('someEvent', myHandler);

function myHandler(arg:String) {
  //...
}

I get a compiler error

error: haxe.extern.Rest<Dynamic> should be String

This is not supported and I have to list all the possible options in @: overload metadata? Or am I doing something wrong?

Because listener is Rest<Dynamic>->Void, but myHandler is String->Void.
Rest<T> allows to call a function with variable set of arguments.
If you need to allow a function reference with any set of arguments, try changing listener type to haxe.Constraints.Function

I thought about it, but it will accept any function. The js code I want to wrap has many functions like this

export function callback (event: string, listener: (... args: string []) => boolean | void): void;

Can I somehow wrap this in an extern safe manner?

You can also change myHandler to function myHandler(args:Rest<String>) {

It is assumed that the handler will be declared in the regular class (not extern).

Unfortunately I don’t see a clean solution.There’s an ongoing effort Rest arguments by RealyUniqueName · Pull Request #9274 · HaxeFoundation/haxe · GitHub But until it’s finished you’ll have to resort to haxe.Constraints.Function.

1 Like

I get it. Thanks for the help. You guys are doing a great job!