How to compile Kotlin into TypeScript?
Asked Answered
B

2

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.

Beshore answered 7/1, 2019 at 21:44 Comment(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... – Ameba
I need the whole code in ts, not just the model – Beshore
Consider full-stack TypeScript from the start. Just a suggestion, might not be practical for you / your team / your use case 🌹 – Transude
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. – Router
If you are looking for an easy way to try out ts-generator, you can try the ktsgenerator Gradle plugin – Alben
@Alben 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 ? – Beshore
M
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

Margemargeaux answered 14/1, 2019 at 17:14 Comment(2)
1) JavaScript is valid TypeScript after small changes in syntax; 2) Kotlin can produce JavaScript. – Carry
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. – Charlotte
A
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.

Attu answered 7/1, 2020 at 16:23 Comment(4)
can you give us the name of the gradle plugin? – Basile
plugins.gradle.org/plugin/net.akehurst.kotlin.kt2ts though newer versions of kotlin have their own ts generation, this is not maintained anymore – Attu
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 – Basile
I get several answers from google! [kotlinlang.org/docs/js-overview.html#kotlin-js-frameworks] [#42538177 – Attu

© 2022 - 2024 β€” McMap. All rights reserved.