https://kotlinlang.org logo
#javascript
Title
# javascript
e

Edoardo Luppi

02/21/2024, 10:36 AM
How do I translate this TS declaration to Kotlin externals?
Copy code
interface Example {
  "example-key": string;
}
a

andylamax

02/21/2024, 10:39 AM
Copy code
external interface Example {
  @JsName("example-key")
  var exampleKey: String
}
doesn't this work??
e

Edoardo Luppi

02/21/2024, 10:40 AM
Doesn't compile as the dash is an illegal char for a variable name in JS
a

andylamax

02/21/2024, 10:40 AM
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

Edoardo Luppi

02/21/2024, 10:43 AM
Yeah I guessed that was one way. Maybe worth opening an issue?
That is very error prone
a

andylamax

02/21/2024, 10:44 AM
Yes, this is worth opening an issue, coz it is an actual Javascript feature that kotlin fails to interop well with JavaScript
a

Artem Kobzar

02/21/2024, 12:46 PM
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

Edoardo Luppi

02/21/2024, 12:50 PM
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

Artem Kobzar

02/21/2024, 12:51 PM
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

Edoardo Luppi

02/21/2024, 12:52 PM
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.