We're writing classes in Kotlin which are (1) reliant on enums, and (2) heavily nested.
We want to call methods that use/return these classes from our Typescript codebase, where enums are defined as a named set of constants: https://www.typescriptlang.org/docs/handbook/enums.html
We find ourselves using
kotlinx.serialization
almost exclusively to map from Kotlin Enum Classes to `Int`/`String`s for JS, because our deeply nested classes make it difficult to handwrite methods that accept primitives and map them to Kotlin enum classes.
• Is there any way to have Kotlin expose
enum class
es as `String`/`Int` values in JS, instead of generated Kotlin classes?
• Will 1.4 be able to expose Kotlin enums as legit Typescript enum constants?
• Is this something that we need `inline enum class`es to solve?
t
turansky
01/22/2020, 10:13 PM
Custom serializer can solve problem TS
Int
<-> Kotlin
enum
turansky
01/22/2020, 10:17 PM
You can create special interface for enums to simplify converting:
Copy code
interface JsEnum {
val data:Int
}
enum class MyEnum(
override data:Int
)JsEnum {
UP(1),
DOWN(2),
// ...
}
a
ankushg
01/22/2020, 11:30 PM
In JS/TS
MyEnum.UP
is still exposed as an object containing
{ data: 1 }
and not a raw integer literal
1
, right?
I already have custom kotlinx.serialization Serializers defined to go from int/string to enums, but it feels like a nontrivial amount of overhead…