Kotlinx Serialization TypeScript Generator (or KxsTsGen for short) creates TypeScript interfaces from Kotlinx Serialization classes, allowing for quick and easy communication via JSON with a Kotlin-first approach.
import kotlinx.serialization.*
import dev.adamko.kxstsgen.*
@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
surprises.
See the docs for working examples.
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 | ✅ example | |
Nullable and default-value properties | ✅ example | |
Value classes | ✅ example | |
Enums | ✅ example | |
Lists | ✅ example | |
Maps | ✅/⚠ example | Maps with complex keys are converted to an ES6 Map, see documentation |
Polymorphism - Sealed classes | ✅/⚠ example | Nested sealed classes are ignored, see documentation |
Polymorphism - Open classes | ❌ example | Not implemented. Converted to type MyClass = any |
@JsonClassDiscriminator |
❌ | Not implemented |
JSON Content polymorphism | ❌ example | Not implemented. Converted to type MyClass = any |
Edge cases - circular dependencies | ✅ example |