13

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.

kzm
  • 373
  • 3
  • 12
sab
  • 4,352
  • 7
  • 36
  • 60
  • Maybe have a look at this: https://github.com/ntrrgc/ts-generator . Why do you what to do this though? TypeScript too transpiles to JavaScript anyway... – Patric Jan 07 '19 at 22:09
  • I need the whole code in ts, not just the model – sab Jan 07 '19 at 23:33
  • Consider full-stack TypeScript from the start. Just a suggestion, might not be practical for you / your team / your use case – basarat Jan 08 '19 at 00: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. – shabunc Jan 22 '19 at 16:29
  • If you are looking for an easy way to try out ts-generator, you can try the [ktsgenerator](https://github.com/ayedo/ktsgenerator) Gradle plugin – Ynv Nov 09 '19 at 14:37
  • @Ynv I will try it on my project, thanks! In the mean time, can you add a real response to this post, so if it work , I change the choosen answer ? – sab Nov 14 '19 at 09:16

2 Answers2

17

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

kzm
  • 373
  • 3
  • 12
  • 1) JavaScript is valid TypeScript after small changes in syntax; 2) Kotlin can produce JavaScript. – user1742529 Oct 20 '19 at 21:34
  • 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. – Arlen Beiler May 20 '22 at 13:51
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.

user2228895
  • 167
  • 7
  • can you give us the name of the gradle plugin? – xetra11 Sep 06 '21 at 18:16
  • https://plugins.gradle.org/plugin/net.akehurst.kotlin.kt2ts though newer versions of kotlin have their own ts generation, this is not maintained anymore – user2228895 Sep 08 '21 at 06:09
  • 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 – xetra11 Sep 08 '21 at 06:57
  • I get several answers from google! [https://kotlinlang.org/docs/js-overview.html#kotlin-js-frameworks] [https://stackoverflow.com/questions/42537677/kotlin-javascript-to-typescript-definition-file] – user2228895 Sep 09 '21 at 14:58