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?
Write in TypeScript, compile for both JVM and browser, try https://1c.wizawu.com
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:
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:
* 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With