JS / External class not converting Javascript Number to Kotlin Long


#1

I have an external JS class:

external class MyJsClass {
    fun number(): Long
}

When I try call this function, the generated code does not convert the Javascript Number to Kotlin Long but simply tries to call Kotlin functions on it, eg.

// Kotlin code
val a: MyJsClass = ...
val b: MyJsClass = ...
a.number().compareTo(b.number())

results in a <TypeError: ... .compareTo_11rb$ is not a function> because the generated code is simply:

// Javascript code
a.number().compareTo_11rb$(b.number())

What should I do to force the conversion to a “real” Kotlin Long?


#2

Using

  • Number instead of Long in the external class definition and
  • explicitly converting to Long

seems to work as expected:

external class MyJsClass {
    fun number(): Number
}

val a: MyJsClass = ...
val b: MyJsClass = ...
a.number().toLong().compareTo(b.number().toLong())

(Edited the original post to match the examples.)