Has anyone tried the generated typescript definiti...
# javascript
s
Has anyone tried the generated typescript definitions yet? I’m going to try it soon with my JS team so was curious if anyone had any experience before I try this next week
s
I’ve tried it 🙂 Works fine for simple declarations, but has a few problems with non-plain module systems, object declarations, enums, nested classes, and couple other cases. We’re working to fix and improve it in near future.
s
Can you explain a bit more? Also - will we see these improvements before the 1.4 stable release?
1
Also - how do extension functions translate?
g
I'm having a problem in casting plain JSON objects coming from fetch requests to actual JS classes with methods from Kotlin such as
.toString(), .copy(), constructor
, because the actual class implementations in the generated .js file append an underscore to all the field names, for example id becomes _id.
d
have you tried kotlinx.serialization?
g
is it possible to use kotlinx.serialization in typescript?
e
@Grantas33 I wonder if when writing Kotlin you could you kotlinx.serialization and when writing code in typescript you could use whatever serialization it provides and since JSON is a standard format, it's highly possible that the two technologies would generate and expect the same JSON form.
s
@Grantas33 if I have a public field name called
id
, it will show up as
_id
to be accessed from typescript?
g
Generated typescript code will have the same field and method names as Kotlin classes. However, the generated JS class which implements the methods of the Kotlin class (such as equals(), toString()) will have its field names prefixed with an underscore. Meaning that in typescript if you would do something like
JSON.stringify(new SampleClass("someId"))
it would return
{"_id": "someId"}
which would fail to deserialize to
data class SampleClass(val id: String)
in Kotlin
s
ic
kotlinx.serialization seems like the right way to fix it
g
How should you serialize/deserialize something using kotlinx.serialization from typescript?
s
you wouldn’t.
Kotlin would be deserialized via kotlinx.serialization
js would be serialized in the Json.stringify manner
g
oh. so basically kotlinx.serialization would have to be configured to correctly parse fields with underscore prefixes?
s
I think it will automatically handle it
g
I tried it but it throws an error
e
What error? Did you figure this out?
s
I tried in M2 and typescript definitions weren’t ready. I haven’t tried in RC, but some of the fixes they have planned aren’t even coming until 1.4.20