Is there any way to use a map in a JsExport annota...
# javascript
q
Is there any way to use a map in a JsExport annotated class and have it be a simple javascript object? For example:
Copy code
@JsExport data class Response(val b: Map<String, Int>)

@JsExport fun response() = Response(mutableMapOf("c", 1))
On the javascript side, I expect something similar to be returned, but instead what I see
{ "b": { "c": 1 } }
When I debug this on the javascript side, I see a lot of properties are
Copy code
{
  "b": {
    "_keys_1": null,
    "_values_1": null,
    "keysView_1": null,
    "valuesView_1": null,
    "entriesView_1": null,
    ... // a lot more properties
  }
}
j
This is coming in 2.x
I can't find the announcement
It was on the blog or somewhere. Still looking
q
Thanks Jake, I'm not sure this works for a
Map<String, V>
, where the keys are not defined at compile time 🤔
t
@JsPlainObject
- annotation for external interfaces. You can use it right now in Kotlin 1.9 with
jso
factory method, which will create required JSO.
If fields are dynamic - you can use
recordOf
function.
external interface +
jso
or factory created by
@JsPlainObject
= JSO with strict fields
Record
interface +
recordOf
factory = JSO with dynamic fields
e
Are you on 1.9, or 2.0?
Artem added the possibility of js-exporting collections to the JS side in 2.0. However, the exposed map isn't a JS Map, or a JS dictionary. JS side example:
Copy code
const mutableMap = provideMutableMap() // Kotlin fun
const mutableMapView = mutableMap.asJsMapView() // Allow JS consumer to modify the Kotlin map