15

I have a JavaScript build in my Kotlin project. How can I produce full TypeScript code instead of JavaScript?

I use Maven; but if you have a Gradle solution, I'm interested.

6
  • Maybe have a look at this: github.com/ntrrgc/ts-generator . Why do you what to do this though? TypeScript too transpiles to JavaScript anyway... Commented Jan 7, 2019 at 22:09
  • I need the whole code in ts, not just the model Commented Jan 7, 2019 at 23:33
  • Consider full-stack TypeScript from the start. Just a suggestion, might not be practical for you / your team / your use case 🌹 Commented Jan 8, 2019 at 0:10
  • What exactly are you trying to achieve? It's not clear from what you are asking. Do you want to have an opportunity to use typescript alongside with kotlin? This is doable. Or you want to compile kotlin to typescript? This is a different story. Commented Jan 22, 2019 at 16:29
  • If you are looking for an easy way to try out ts-generator, you can try the ktsgenerator Gradle plugin Commented Nov 9, 2019 at 14:37

2 Answers 2

19

UPDATE: Preview of TypeScript definition generation is available in IR backend since Kotlin 1.4

From Kotlin documentaion:

The Kotlin/JS IR compiler is capable of generating TypeScript definitions from your Kotlin code. These definitions can be used by JavaScript tools and IDEs when working on hybrid apps to provide autocompletion, support static analyzers, and make it easier to include Kotlin code in JavaScript and TypeScript projects. Top-level declarations marked with @JsExport in a project that produces executable files (binaries.executable()) will get a .d.ts file generated, which contains the TypeScript definitions for the exported Kotlin declarations. In Kotlin 1.4, these declarations can be found in build/js/packages/<package_name>/kotlin alongside the corresponding, un-webpacked JavaScript code


Kotlin/JS compiler can't produce TypeScript code. There are no plans to generate full TypeScript code. And there are no third-party solutions as far as I know.

However, we plan to produce TypeScript definition files alongside JavaScript code: KT-16604

Sign up to request clarification or add additional context in comments.

2 Comments

1) JavaScript is valid TypeScript after small changes in syntax; 2) Kotlin can produce JavaScript.
Javascript contains less information than Typescript. You should be able to go directly from Kotlin to Typescript, but going through Javascript first removes TypeScript information and then infers it from the Javascript code.
1

I wrote a plugin for gradle to assist with kotlin - typescript integration. It basically generates the .d.ts files for you.

Issues discussed here https://medium.com/@dr.david.h.akehurst/building-applications-with-kotlin-and-typescript-8a165e76252c

Gradle plugin is available in the gradle plugin registry.

4 Comments

can you give us the name of the gradle plugin?
plugins.gradle.org/plugin/net.akehurst.kotlin.kt2ts though newer versions of kotlin have their own ts generation, this is not maintained anymore
Oh. How can I use the native generation of those newer versions? I use IntelliJ and could not find any feature that does that for me and google only brings me to these questions here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.