Skip to content
 
 

Repository files navigation

GitHub license Maven Central

Kotlinx Serialization TypeScript Generator (kotlin-tsgen)

Note

Maintained continuation: This repository is a fork of Kotlinx Serialization TypeScript Generator (KxsTsGen), building on its original implementation while continuing development and releases under the io.github.esafak namespace.

kotlin-tsgen creates TypeScript interfaces from kotlinx.serialization classes, allowing for quick and easy communication via JSON with a Kotlin-first approach.

import kotlinx.serialization.*
import io.github.esafak.kotlintsgen.*

@Serializable
class MyClass(
  val aString: String,
  var anInt: Int,
  val aDouble: Double,
  val bool: Boolean,
  private val privateMember: String,
)

fun main() {
  val tsGenerator = KotlinTsGenerator()
  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 surprises.

See the docs for working examples.

Status

Status Notes
Kotlin multiplatform ✅ JVM and JS (Node.js and browser) tests run in CI
@SerialName ✅ example Names used as TypeScript identifiers are validated; invalid names fail generation with a clear error
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 subclasses are flattened; see notes
Polymorphism - Open classes ✅/⚠ example Registered subclasses in a SerializersModule generate a TypeScript union; otherwise falls back to type MyClass = any
SerializersModule contextual serializers ✅/⚠ Registered serializers are resolved; generic providers requiring type arguments fall back to any
@JsonClassDiscriminator ✅ example Selects the discriminator property and generated enum name for sealed hierarchies; kotlinx.serialization requires subclass values to match
Namespaces ✅ example Disabled by default; static and descriptor-name-prefix namespaces are supported
JSON Content polymorphism ✅ example Explicit subtype mappings generate a plain TypeScript union
Edge cases - circular dependencies ✅ example

About

kotlin-tsgen :: Generate TypeScript interfaces from Kotlin classes

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages