Chris Miller
10/08/2024, 10:56 PMexport type TextLayer = {
"id": string;
"font-name"?: string;
"text-size"?: number;
}
What's the correct way to map that to Kotlin/JS, so that I can create instances via jso { ... }
? I've tried the following, but the @JsName
annotations fail with "Name contains illegal chars that cannot appear in JavaScript identifier":
@JsPlainObject
external interface TextLayer {
var id: String
@JsName("font-name")
var fontName: String?
@JsName("text-size")
var textSize: Double?
}
Also, is the use of @JsPlainObject
relevant here? The Kotlin/JS documentation seems quite sparse, plus I can't find any documentation for most of the @Js*
annotations, so feel like I am shooting in the dark a bit here. Any pointers would be greatly appreciated!turansky
10/09/2024, 12:30 AMfail with "Name contains illegal chars that cannot appear in JavaScript identifier":OOB support is coming in Kotlin 2.1 For now you need compiler option like here
turansky
10/09/2024, 12:31 AMAlso, is the use ofOn 100%. Andrelevant here?@JsPlainObject
val
instead of var
for properties.Chris Miller
10/09/2024, 7:35 AM@JsSpecialName
annotation too which also seemed to work - is that equivalent to JsName
with -XXLanguage:+JsAllowInvalidCharsIdentifiersEscaping
, or is it intended for something else?
As a more general question, is any of this (or many other parts of Kotlin/JS) properly documented anywhere? I generally seem to have to resort to searching this channel or cobble together my understanding from example code and comments scattered around the internet 😞Chris Miller
10/09/2024, 8:23 AMJsPlainObject
at leastturansky
10/09/2024, 9:47 AMis that equivalent toNo,withJsName
, or is it intended for something else?-XXLanguage:+JsAllowInvalidCharsIdentifiersEscaping
@JsName
is what you need. @JsSpecialName
- temp WA for type guardsChris Miller
10/09/2024, 9:50 AMturansky
10/09/2024, 9:54 AM@Js*
annotation.
Common practices you can find in Kotlin Wrappers and examples inside.Chris Miller
10/09/2024, 11:09 AMturansky
10/09/2024, 11:17 AMChris Miller
10/09/2024, 11:18 AMChris Miller
10/10/2024, 9:01 PM@JsName
properly, but it doesn't seem to be working. My interface:
@JsPlainObject
external interface TextLayer {
val id: String
@JsName("font-name")
val fontName: String?
}
My code using it:
@Composable
fun Test() {
Div {
Text(JSON.stringify(TextLayer(id="123", fontName="abc")))
}
}
The output:
{
"id": "123",
"fontName": "abc"
}
If I disable -XXLanguage:+JsAllowInvalidCharsIdentifiersEscaping
then it won't even compile, so that flag does at least appear to be having some effect. I'm on the latest IDEA 2024.3 EAP in K2 mode, and am using Kotlin 2.0.20. Not sure what I'm missing?
Edit: the following does work (if I change both the val
back to var
) which is probably why I thought @JsName
was working earlier:
Text(JSON.stringify(jso<TextLayer> { id="123"; fontName="abc" })) }
output:
{
"id": "123",
"font-name": "abc"
}
Chris Miller
10/14/2024, 7:18 PMjso {}
since I can't find a way to get JsPlainObject
and JsName
to play nicely together with the +JsAllowInvalidCharsIdentifiersEscaping
flag. I can file a bug report, but maybe you know if this is already fixed/implemented in 2.1.0?turansky
10/14/2024, 10:22 PM2.1.0-Beta2
before report