Kotlinx Serialization TypeScript Generator
Kotlinx Serialization TypeScript Generator creates TypeScript interfaces from Kotlinx Serialization classes.
@Serializable
class MyClass(
val aString: String,
var anInt: Int,
val aDouble: Double,
val bool: Boolean,
private val privateMember: String,
)
fun main() {
val tsGenerator = KxsTsGenerator()
println(tsGenerator.generate(MyClass.serializer()))
}
Generated TypeScript interface:
export interface MyClass {
aString: string;
anInt: number;
aDouble: number;
bool: boolean;
privateMember: string;
}
Only Kotlinx Serialization SerialDescriptor
s are used to generate TypeScript. They are flexible and comprehensive enough to allow for accurate TypeScript code, without any deviation.
See the docs for working examples.
Status
This is a proof-of-concept.
The aim is to create TypeScript interfaces that can accurately produce Kotlinx Serialization compatible JSON.
Status | Notes | |
---|---|---|
Kotlin multiplatform |
|
The codebase is multiplatform, but only JVM has been tested |
@SerialName |
|
The serial name is directly converted and might produce invalid TypeScript |
Basic classes |
|
|
Nullable and default-value properties |
|
|
Value classes |
|
|
Enums |
|
|
Lists |
|
|
Maps |
|
Maps with complex keys are converted to an ES6 Map, see documentation |
Polymorphism - Sealed classes |
|
Nested sealed classes are ignored, see documentation |
Polymorphism - Open classes |
|
Not implemented. Converted to type MyClass = any |
@JsonClassDiscriminator |
|
Not implemented |
JSON Content polymorphism |
|
Not implemented. Converted to type MyClass = any |
Edge cases - circular dependencies |
|