Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert TypeScript to Java

I've been writing TypeScript recently, for running in a browser. What I'm finding I want to do is run the same code from within a Java application. I have a complex piece of logic I want to write once and use in a JavaScript environment within a browser, and within a Java desktop environment. Now I know that I can host javascript from within java, but what I'm wondering is whether there is a way of converting TypeScript into pure java code?

like image 257
Tom Quarendon Avatar asked Nov 16 '15 17:11

Tom Quarendon


2 Answers

Write in TypeScript, compile for both JVM and browser, try https://1c.wizawu.com

like image 124
wizawu Avatar answered Oct 21 '22 00:10

wizawu


Apologies if this appears to introduce yet another language into the mix.

Why not consider using ScalaJS to both generate JavaScript AND JVM-compatible (Java) routines, therefore taking advantage of Java-to-Scala interop and Java-like syntax?

Basically, a Scala/ScalaJS code project can be configured to output both Javascript and Java-compatible byte code libraries/JARs (the latter for integration back into other Java/JVM code).

If learning the Scala language seems like yet another time-sink, you could also consider programmatically converting your existing Java routines into Scala*, for further conversion into ScalaJS/Javascript, all as an extra build step:

  • Scalagen - Java to Scala conversion (try online here)

Finally, you can, with some caveats, convert 3rd-party TypeScript defintions into Scala[JS] code API's, so as to make calls to included TypeScript libraries strongly-typed Scala code:

  • scala-js-ts-importer - TypeScript Importer for Scala.js

* ScalaJS code can only depend-on/cross-compile other Scala-derived code/libraries, but 'normal' Scala can import any Java-compatible library.

UPDATE: Kotlin, by Jetbrains (authors of various great IDEs, including IntelliJ IDEA) is another JVM-centric, concise, language, also with Javascript cross-compilation support. There’s also a one-click Java-to-Kotlin converter tool, for both Intellij and Eclipse.

Kotlin seems to be gaining traction, is said to be less 'complex' than Scala, and is also being used for Android and IOS app development (RoboVM Studio).

You can try out Kotlin here.

like image 23
Big Rich Avatar answered Oct 20 '22 22:10

Big Rich