Hey everyone :wave:, I've run into a quirky behavi...
# javascript
a
Hey everyone 👋, I've run into a quirky behavior with Kotlin/JS. Curious to know if anyone has seen this before or if it's just the norm? I've got a
data class
inheriting from a
sealed class
. When I override a field in the data class and then
console.log()
the instance in JS, the overridden field appears to have an obfuscated name.
Copy code
@OptIn(ExperimentalJsExport::class)
@JsExport
data class Demo(override val id: String) : DemoParent()

@JsExport
sealed class DemoParent() {
    abstract val id: String
}
Copy code
fun main() {
    val demo = Demo("myId")
    console.log(demo)
}
Console shows:
Copy code
Demo {q5q_1: 'myId'}
   q5q_1: "myId"
   id: "myId"
   [[Prototype]]: DemoParent
Notice the
id
property also has this
q5q_1
version? 🤔 I'm using
dev.petuska.npm
for npm publishing. Has anyone come across this? Is it a Kotlin/JS issue? Or maybe something with the
dev.petuska.npm
plugin?
a
Hi 👋 I've just tried your example on playground (https://pl.kotl.in/GtpDRLJbr) and realized that it's not actually a field in your object. Seems like (at least I realized it in Chrome) it also shows to you defined properties inside prototype. For the @JsExport class in your code we generate field to store the value and getter getter function that is used in the code. But also, for JS consumption we generate a readonly (because
id
is
val
) property on prototype with a stable name. Seems like Chrome/Node also show this property as an objects' field
a
So this mean that for every overridden property, they will be always to fields, one with stable name from parent (prototype) and the other one with dynamic name (from child itself).
a
Yes, but it's only for exported classes
And from parent it will be only one (from the first exported parent) and will not copy into every child class
a
Got it. And there is no way to supply stable name to these child class overridden fields or stop export of them ?
a
No, unfortunately.
a
Thanks for the help here 🙂