https://kotlinlang.org logo
Title
k

Kemal Erbakirci

02/18/2023, 9:34 AM
hi, i’m an old kotlin for java user but wanted to try kotlin for js first time for a node.js project. but it doesn’t go smooth! Unfortunately I keep getting errors. here is my code:
@JsModule("cdktf")
@JsNonModule
open external class App()

fun main() {
    val app = App()
    println(app)
}
and here is the error message:
val app = App()
              ^
TypeError: App is not a constructor
any help will be highly appreciated
b

Big Chungus

02/18/2023, 9:45 AM
Have a read through this to get a better understanding of kotlin externals https://dev.to/mpetuska/js-in-kotlinjs-c4g
Js has many module systems so wrapping approaches differ wildly across libs
k

Kemal Erbakirci

02/18/2023, 9:47 AM
thanks @Big Chungus i’m gonna read
this is using a ts lib from npm
b

Big Chungus

02/18/2023, 9:48 AM
Doesn't matter since ts only lives during build time. The lib still ends up being js after publishing.
k

Kemal Erbakirci

02/18/2023, 9:49 AM
cool, i started reading the post
hi again, after reading your post, i had some improvement, thanks a lot. but i’m again stuck 🤦‍♂️ here’s the current version of the code:
import constructs.Construct

@JsModule("constructs")
@JsNonModule
external object constructs {
    open class Construct
}

@JsModule("cdktf")
@JsNonModule
external object cdktf {
    class App : Construct {
        fun synth(): Unit

    }
    open class TerraformStack(scope: Construct, id: String) : Construct
    class TerraformVariable(app: Construct, id: String, function: () -> Unit)
}

//////////////
class MyStack(scope: Construct, id: String) : cdktf.TerraformStack(scope, id) {
    init {
        cdktf.TerraformVariable(this, "my_var") {}
    }
}

fun main() {
    val app = <http://cdktf.App|cdktf.App>()
    MyStack(app, "ctd-infra")
    app.synth();
}
and now I get this error:
class MyStack(scope: Construct, id: String) : cdktf.TerraformStack(scope, id) {
                                              ^
ReferenceError: TerraformStack is not defined
any ideas?
b

Big Chungus

02/18/2023, 6:16 PM
Looks like incorrect declarations. I'd need to see js/ts code you're wrapping to advise more
k

Kemal Erbakirci

02/20/2023, 8:32 AM
hi, i saw this just now. this is the declaration of that class: https://github.com/hashicorp/terraform-cdk/blob/main/packages/cdktf/lib/terraform-stack.ts#L61
b

Big Chungus

02/21/2023, 12:22 AM
Ok, did some digging in my sandbox and it looks to be a bug related to extending external classes KT-56818. Everything works just fine if you don't try to extend the external class and just use it directly.
Here's my code if you want to play with it https://github.com/mpetuska/kotlin-sandbox/tree/2a7d467fa0f42baa751e8c68433ad9c45dcb306e/modules/kjs As a temporary workaround until it's fixed I could suggest using delegation over inheritance when bridging with external classes.
k

Kemal Erbakirci

02/21/2023, 6:25 PM
thanks a lot Martynas for your helps! tbh, it is way more complicated than i expected…
b

Big Chungus

02/21/2023, 6:34 PM
It is, unfortunately. Steep learning curve, but once you get a hang of it it becomes a lot easier.
Js having shitloads of ways to export things doesn't help either
k

Kemal Erbakirci

02/21/2023, 6:36 PM
for me this is not
kotlin
😄
b

Big Chungus

02/21/2023, 6:37 PM
Agree that it's bonkers if you have to do it yourself all the time. The saving grace is the oss community that wraps popular js libs and publishes them to maven central.