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?
-
Why? TypeScript's raison d'être is to add static types to JS. Java already has static types, I'm unsure what the benefit is. – Ruan Mendes Nov 16 '15 at 19:32
-
2@Juan Mendes. So that I can write a piece of complex logic once and share it in both environments. Efficiently. – Tom Quarendon Nov 17 '15 at 01:19
-
1GWT may be a different solution to the same problem, write the logic in Java and have it compiled into JavaScript using GWT. – Tom Quarendon Nov 17 '15 at 01:21
-
2If you wanted to be really adventurous, you could fork TypeScript and write a custom component to plug into the emit phase of TypeScript's compiler to output Java instead of JavaScript. This though I suspect would be far more complex than maintaining both a Java and TypeScript version of your logic. You might have more success doing it the other way around, transpiling Java into JavaScript. EDIT: one more alternative would be to execute your compiled TypeScript (JavaScript) from within Java, I believe there are libraries that do this. – amg-argh Nov 17 '15 at 01:27
-
@amg-argh. Yes, I know I can host javascript from within Java, the javax.script package does that and uses either Rhino or now Nashorn. – Tom Quarendon Nov 17 '15 at 16:13
-
@TomQuarendon my comment was more probably regarding, that an overall better solution for you would be to "Run/Execute" the JS from Java, rather than trying to transpile into Java. – amg-argh Nov 17 '15 at 19:13
-
I would like the same thing, do let us know if you found a transcompiler from typescript to java? – tgkprog Aug 04 '21 at 15:02
4 Answers
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.
-
3The reason of that it surely that the OP wants to develop typescript and not scalajs. Your answer is very useful - but only for a different question. – peterh May 23 '17 at 13:57
TypeScript is a still new language and it adds features rapidly. Even if there was such a transpiler, you would end up in a very unfortunate situation because the transpiler would be always behind TypeScript capabilities.
A "safer" approach would be to transpile TypeScript to JavaScipt and that transpile to Java. It makes more sense to me because JavaScript is a standardized language that reached some kind of maturity but it would still be a bad idea. However, there is not even a transpiler for the latest step.
Even if there were such transpilers, I would definitely not want to do that because you would end up in a trap. Transpilers are by no means perfect and the code would be harder for people to understand (example) and to verify.
Edit: Mozilla Rhino project can translate JavaScript to Java.
-
-
If the converter generated output as readable as the typescript to javascript compiler does, I'd be very happy. I find that quite readable – Tom Quarendon Nov 17 '15 at 01:09
-
By converting JavaScript you would loose all the rich typing information that you have in a TypeScript program, so I don't see why that would be a better idea. – Tom Quarendon Nov 17 '15 at 01:12
-
In fact Mozilla Rhino will convert javascript into Java, so such a compiler for JavaScript DOES exist. – Tom Quarendon Nov 17 '15 at 01:13
-
3https://en.wikipedia.org/wiki/Rhino_(JavaScript_engine) - *Rhino is a JavaScript engine written fully in Java.* So it does not convert JavaScript to Java but it compiles JavaScript code to run it. – MartyIX Nov 17 '15 at 06:19
-
2*By converting JavaScript you would loose all the rich typing information that you have in a TypeScript program, so I don't see why that would be a better idea.* -> Because I don't believe it is a good idea to modify the resulting code. You couldn't make any modifications in the original TypeScript code then. Ultimately, this question is subjective and my experience is that you'll end up in a lot of trouble in a long run with this approach. It's not that I wish you fail with your endeavor, it's just that I've already tried some of these paths and they led to obscure problems. – MartyIX Nov 17 '15 at 06:26
-
Rhino JavaScript compiler: "The JavaScript compiler translates JavaScript source into Java class files. The resulting Java class files can then be loaded and executed at another time, providing a convenient method for transferring JavaScript, and for avoiding translation cost." – Tom Quarendon Nov 17 '15 at 16:11
-
@ Martin Vseticka: I don't see why I'd modify the resulting java code, or why I would then be prevented from modifying the Typescript. – Tom Quarendon Nov 17 '15 at 16:15
-
-
LLVM is another option. There are github and other efforts to compile typescript to LLVM. Seems associated with web assembly, too. And it is the base to a lot of Java VMs. – TamusJRoyce Feb 01 '19 at 00:54
I have a similar goal and am about to try http://www.jsweet.org/ to transpile java to javascript. So far there does some to be some limitations (no support for the diamond operator, anonymous inner classes, etc), but the benefits to being able to write and maintain one codebase instead of two are too big to ignore

- 7,682
- 5
- 22
- 30
-
2Not this was asked for. The OP asked for typescript to java, and not java to javascript. – peterh May 23 '17 at 13:56