Skip to content

Generate TypeScript from Kotlin (or any jvm language) by annotations

License

Notifications You must be signed in to change notification settings

jensim/kt2ts-gradle-plugin

Repository files navigation

Kotlin to TypeScript gradle-plugin

Have your backend declare the data types (DTOs) for its API, and use em in the front end. Great when working with Angular or other TypeScript frameworks.

This project is a wrapper around the ts-generator project

Project Status: Active – The project has reached a stable, usable state and is being actively developed. License Gradle Plugin Portal Build Status Quality Gate Status Coverage Lines of Code Maintainability Rating

Usage

build.kotlin.kts

plugins {
    id("se.jensim.kt2ts") version "$pluginVersion"
}

kt2ts {
    // Repeatable block for linking outputfile to a set of annotations
    output {
        outputFile = file("$buildDir/ts/kt2ts.d.ts")
        annotations = listOf("com.example.ToTypescript")
    }
    classFilesSources {
        // Two ways of setting classes dir, if both are set, both are jointly used
        // One has to be provided (for task input resolution to work properly, I made it mandatory)
        compileTasks = listOf(tasks.compileKotlin, tasks.compileJava)
        classesDirs = files("$buildDir/classes/kotlin/main")
    }
}
// Shorthand versions - with default values
kt2ts {
    annotation = "com.example.ToTypescript"
    classFilesSources.compileTasks = listOf(tasks.compileKotlin, tasks.compileJava)
}
kt2ts {
    annotations = listOf("com.example.ToTypescript")
    classFilesSources.compileTasks = listOf(tasks.compileKotlin, tasks.compileJava)
}

Your custom annotation and API data classes

package com.example

annotation class ToTypescript

@ToTypescript
data class OneDataType(val types: List<ThreeDataType>, val e: FouthEnum)

data class TwoDataType(val name: String)

data class ThreeDataType(val name: String)

enum class FouthEnum {
    FOUR
}

Dependencies and license stuff

Dependency License
kt2ts (this) License
ts-generator License
reflections WTFPL
javassist (transitive) License: MPL 2.0
guava (transitive) License

Why?

Because we need something like kt2js https://youtrack.jetbrains.com/issue/KT-16604

Tests & CI

We use jacoco for testcoverage of the plugin project, on top of that we build and test the example project that uses the plugin when building master and tagging releases in TravisCI. All plugin publishings to gradle plugin protal are made in trvisCI after a full build & test has been successful in both the plugin and the example project.