Koriel / kotlinx-serialization-typescript-generator

kxs-ts-gen :: Generate TypeScript interfaces from Kotlin classes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Status GitHub license Maven Central

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 SerialDescriptors 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 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

About

kxs-ts-gen :: Generate TypeScript interfaces from Kotlin classes

License:Apache License 2.0


Languages

Language:Kotlin 100.0%