hi, i’m an old kotlin for java user but wanted to ...
# javascript
k
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:
Copy code
@JsModule("cdktf")
@JsNonModule
open external class App()

fun main() {
    val app = App()
    println(app)
}
and here is the error message:
Copy code
val app = App()
              ^
TypeError: App is not a constructor
any help will be highly appreciated
b
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
thanks @Big Chungus i’m gonna read
this is using a ts lib from npm
b
Doesn't matter since ts only lives during build time. The lib still ends up being js after publishing.
k
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:
Copy 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:
Copy code
class MyStack(scope: Construct, id: String) : cdktf.TerraformStack(scope, id) {
                                              ^
ReferenceError: TerraformStack is not defined
any ideas?
b
Looks like incorrect declarations. I'd need to see js/ts code you're wrapping to advise more
k
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
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.
🙌 1
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
thanks a lot Martynas for your helps! tbh, it is way more complicated than i expected…
b
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
for me this is not
kotlin
😄
b
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.
1