Alan Humphrey Alan Humphrey - 4 months ago 17
Dart Question

Dart js-interop and overloaded methods

How do you handle interfacing with JS libraries that have overloaded methods?

For example Leaflet.js has both of the following defined for the Map object:

openPopup(popup); // opens the given popup
openPopup(html, LatLng, popOptions); // creates a popup with the html at the location, using the popup options.


What I've come up with is:

@JS("L.Map")
class Map {
/* code */
external Map openPopup(dynamic popup, [LatLng coords, PopupOptions opts]);
/* code */
}


Is there a better way? Note: this seems to work but the analyzer complains: The method openPopup is not defined for the class Map.

Dart: 1.17.1

package:js-0.6.0

Answer

So far I have not been able to specify a different name for an instance member/method using the JS() directive which is a big issue especially for javascript object that have method names that clash with Dart keywords (such as 'catch' in a javascript Promise). I ended up using plain dart:js. And anyway even when using package/js, I ended up adding another layer to make the api more dartish (especially with callbacks and promise) especially to enforce argument types.

What I would expect would be to be able to do (in your example)

@JS("L.Map")
class Map {
   JS('openPopup')
   external Map openPopupHtml(String html, [LatLng coords, PopupOptions opts]);
   JS('openPopup')
   external Map openPopup(Popup popup);
}

but that does not seem to work. Maybe should it be considered as a feature enhancement.