Fixing "You may not export a method of a subclass of js.Any"

246 Views Asked by At

I am trying to create a binding based on typescript type information to a Javascript library (underscore) for Scala.js. It contains some methods like the following:

/**
* Create a shallow-copied clone of the object.
* Any nested objects or arrays will be copied by reference, not duplicated.
* @param object Object to clone.
* @return Copy of `object`.
**/
clone<T>(object: T): T;

The problem is, when I try to translate this to Scala.js:

@JSExport("clone")
def cloneJS[T](`object`: T): T = js.native

I get the following error:

You may not export a method of a subclass of js.Any

(Similar things happen when I tried to do the same with a toString() method.)

Is it safe to skip translating these methods as Scala can handle them or there is a way to fix this method implementation?

(I am using Scala.js 0.6.3.)

1

There are 1 best solutions below

0
On BEST ANSWER

@JSExport (and related annotations) are designed to export methods and properties of Scala objects to JavaScript. Here, you are defining an interface for a JavaScript object for use by Scala.js, so exporting doesn't mean anything.

I see you're trying to give a different name with the annotation. For that purpose, in facade types, there is another annotation: @JSName. So the proper definition of your method should be:

@JSName("clone")
def cloneJS[T](`object`: T): T = js.native

and everything will be fine.

Btw, in this instance, nothing prevents from defining the Scala method with the name clone in the first place (since there is one argument, it will be different from the Object.clone() method):

def clone[T](`object`: T): T = js.native

will work just as well, and will probably be nicer at call site.