I'm trying to link with an external JS library usi...
# javascript
a
I'm trying to link with an external JS library using ts2kt. Typescript definition has a class with constructor. Constructor has an argument of type
Config
. There is also
declare type Config = { ... }
. In kotlin code
Config
becomes an external interface with plenty of optional vars. How should I create an instance of this interface? Just like this?
Copy code
val cfg = object : Config {
    override var width: dynamic
        get() = 200
        set(value) {}
    override var height: dynamic
        get() = 300
        set(value) {}
    ...
}
It looks monstrous for me...
r
Hi Alexey, you could make it shorter like
Copy code
override var width = 200
        set(value) {}
    override var height = 300
        set(value) {}
Anyway it seems like you need a custom setter so it should be declared explicitly
a
To be honest I'd like to have a ready to use class, not interface, for this purpose. Just to write
Config(200, 300)
.
Or is this impossible due to multiple inheritance of type definitions in typescript?
r
I got you, give me a minute
Probably you could do something like
Copy code
@JsName("Config")
external interface _Config

class Config(...): _Config {
 ...
}
Is this working for you?
a
That's all about defining my class Config and linking it to the original in JS. I thought I can use pure result of ts2kt transformation to call JS part from kotlin.
Actually I probably won't go this way, because ts2tk generated tons of "unimplemented warnings" and also compilation errors in the resulting kotlin code.
r
It is true in general but sometimes the result of ts2kt transformation requires a bit modification
a
Is ts2kt being developed right now? Or has it reached some limitations of kotlin language?
b
Right now you can write something like:
Copy code
val cfg = object : Config {
    override var width = 200
    override var height = 200
}
About “a ready to use class” — we are thinking how to do it better. Feel free to vote or star the issue to get updates: https://youtrack.jetbrains.com/issue/KT-21653
you can write something like that:
Copy code
fun <T> newJsObject(): T = js("{}")

val x = newJsObject<FooBar>().apply {
    width = 200
    height = 100
}
Is ts2kt being developed right now?
Yes, we going to continue working on it. Unfortunately, last few months it wasn’t getting enough attention.
a
Is there a way to use a plain-JS-object as an argument to a Kotlin/JS method and easily convert it to an instance of any particular Kotlin class? Or would we need to manually write something getting from an instance of
Json
and pass it into a constructor for a real Kotlin class?
if we can't yet define classes that compile to plain objects, it'd be nice to at least convert back and forth from them with minimal boilerplate (if that's possible)