How do I translate this TS declaration to Kotlin e...
# javascript
e
How do I translate this TS declaration to Kotlin externals?
Copy code
interface Example {
  "example-key": string;
}
a
Copy code
external interface Example {
  @JsName("example-key")
  var exampleKey: String
}
doesn't this work??
e
Doesn't compile as the dash is an illegal char for a variable name in JS
a
then you have no choice apart from extension functions
Copy code
var Example.exampleKey: String
    get() = asDynamic()["example-key"]
    set(value) {
        asDynamic()["example-key"]=value
    }
e
Yeah I guessed that was one way. Maybe worth opening an issue?
That is very error prone
a
Yes, this is worth opening an issue, coz it is an actual Javascript feature that kotlin fails to interop well with JavaScript
a
Also, you can just try to use the regular escaped identifier:
Copy code
external interface Example {
  val `example-key`: String
}
The only thing, I don't know if the feature is turn on by default, but if it's not, there is a compiler flag like
-XXLanguage:+JsAllowInvalidCharsIdentifiersEscaping
🔝 1
e
Thanks Artem! Looking for that compiler option I found https://youtrack.jetbrains.com/issue/KT-31799 It's exactly what I need.
It's probably worth adding it to the Kotlin/JS website!
a
This is exactly the ticket I fixed with this language feature
It should be turn on by default in 2.0 or 2.1
I could try to make it happen in the next beta
But I need to discuss it))
e
No problem, I'm up to enable it with the flag for now.
🫡 1
@turansky’s plugin also has a compiler transformer that works with a
@JsSpecialName
Just found out looking at the code.