How could I easily define an external variable as ...
# javascript
a
How could I easily define an external variable as a JS object ? The JS Object have multiple parts that could be Records and I wondering if there is a simple solution that doesn't require to bundle the entire kotlinx.wrappers library that I currently don't have. I'm in Kotlin 2.1.0 If I create something like
Prism.languages.mcfunction = object { someProperties... }
it's compiled as a custom type and the library doesn't accepts it
r
dynamic
?
a
No, I think you don't understand, this is what I want to achieve in Kotlin/JS https://prismjs.com/extending.html#language-definitions
a
Are you trying to describe existing object (in JS environment) or want to create a variable which holding an object with a specific shape?
a
I want to edit an existing external object to add a new property that is an object with a specific shape, but I don't want to bother creating 150 interfaces and be exact in typings
It's just for highlighting code on my website so I don't care about being very precise on the typings
r
a
Thanks, it works great !
l
I used to do that, but code inside
obj
tend to be in
dynamic
context, leading to error messages being inhibited. I moved to this instead:
Copy code
fun jsObject(vararg fields: Pair<String, dynamic>): dynamic {
    val res = js("{}")
    fields.forEach { (key, value) ->
        js("res[key] = value")
    }
    return res
}
Used like so:
Copy code
val obj = jsObject(
    "foo" to "bar,
    "test" to 1234)