How to set Javascript's function reference in Kotlin?


I think I am having a very similar issue like this. The problem is that the generated type is final.

Here is a part of the TypeScript header:

interface ChartEvents {
    selection?(event: ChartSelectionEvent): void;

Here is the generated Kotlin external interface

external interface ChartEvents {
    val selection: ((event: ChartSelectionEvent) -> Unit)? get() = definedExternally

Here is how I can create a JS interface (calling JS from Kotlin)

js("{ selection: function(event) { console.log("Called") } }")

How can I pass in a Kotlin lambda to selection?

I cannot use function variables, because their names are changed when the final .js file is created.

I have been stuck way too long on this issue. Thank you for your help.


What I did not realize is that val can be changed to var in the binding and it will still work.

external interface ChartEvents {
    var selection: ((event: ChartSelectionEvent) -> Unit)? get() = definedExternally; set(value) = definedExternally

And this is how to create a working copy with type safety:

fun <T> jsc() = js("new Object()") as T

val events = jsc<ChartEvents>().apply { selection = { println("Simple kotlin version") } }