OK, excuse me, visibly it's not a kotlin issue, bu...
# javascript
y
OK, excuse me, visibly it's not a kotlin issue, but a js one.
a
Just to confirm: the way Kotlin handles
@JsName
is not an issue for you then? The
foo
object does have a property
id_a16eoc$_0
, but Kotlin expression
foo.id
should translate to
foo.id
in JS and delegate to the actual value at run time.
y
Well I worked around the issue for my case. But I think it could be an issue for some interrop case. If you use a JS lib which explicitly forbid other type than plain JS object. And you, you want to use a fully featured class in your App. You have no nice way to convert your class to a plain object. You can of course define an interface with the right field, and do
jsObject<TheInterface> { ... }
an copy fields manually but it's not very convenient and very error prone. I think that offering
toPlainJsObject()
or something like that as an extension method would be great. Of course there is
asJsObject()
from https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-extensions/src/main/kotlin/kotlinext/js/Object.kt ; but as it just do a cast, it's not the intended behavior (the name that I give to the field is not the name in the
JsObject
.
a
> If you use a JS lib which explicitly forbid other type than plain JS object
In that case it could be painful indeed. For the record, could you give an example? Better yet, could you file an issues here: https://youtrack.jetbrains.com/issues/KT
Basically copying what you've written and changing a few words would suffice
Unfortunately we don't have a solution for those right now. But it is important to document the issues so that we don't miss them when we revisit the interop design. 😃
y
Well so I think I don't need to fill an issue. It's already documented.
a
Is it React Native you are having issues with?
y
nope, it's in the browser, and the lib requiring plain object is redux.
I finally used an interface which as ability to wrap itself as an attribute in a plain object :
Copy code
interface ClassAction<T : Any> : Action<KClass<out T>> {
    override var type: KClass<out T>
    var appAction: T

    fun toPlainAction(): ClassAction<T> = jsObject {
        this.type = this@ClassAction.type
        this.appAction = this@ClassAction.appAction
    }
}
. Then I write what redux call a
Middleware
to automatically call
toPlainAction
before it is passed to the api which require plain object.
2 weeks ago, my frontend skill was limited to HTML 3.2. Today, I have a kotlin front end app which use react-dom, react-router and redux (uikit for presentation/css and webpack for build and hot reload). I'm pretty proud :).
a
Looks bothersome =( Seems like we might have to introduce some automatic conversions for some cases including this one. Could you mention redux in the comments to one of those issues? Thanks!
Oh, congrats! 😃
y
Thanks
a
Thanks !
d
a
@danfma 👍 Yeah, that makes creating plain JS objects look nice. Unfortunately it doesn't eliminate the boilerplate completely =(
d
You're right.