Giter Site home page Giter Site logo

adamko-dev / kotlinx-serialization-typescript-generator Goto Github PK

View Code? Open in Web Editor NEW
49.0 1.0 5.0 1.2 MB

KxsTsGen :: Generate TypeScript interfaces from Kotlin classes

Home Page: https://adamko-dev.github.io/kotlinx-serialization-typescript-generator/

License: Apache License 2.0

Kotlin 100.00%
kotlin kotlinx-serialization dukat kotlin-library kotlin-multiplatform kotlin-js kotlin-typescript typescript

kotlinx-serialization-typescript-generator's Introduction

GitHub license Maven Central Maven metadata URL

Kotlinx Serialization TypeScript Generator

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.