Convert TypeScript to Java

I recently wrote TypeScript to work in a browser. What I find I want to do is run the same code from a Java application. I have complex logic that I want to write once and use in the JavaScript environment in the browser and in the Java desktop environment. Now I know that I can host javascript from java, but I am wondering if there is a way to convert TypeScript to pure Java code?

+8
java typescript
source share
4 answers

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

+4
source share

Sorry if this means adding another language to the mix.

Why not consider using ScalaJS to generate JavaScript and JVM-compatible (Java) routines, so using Java-to-Scala interop and Java- type syntax

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

If learning the Scala language seems like another temporary stream, you can also consider programmatically converting existing Java routines to Scala * , for further conversion to ScalaJS / Javascript, all this as an additional build step:

Finally, with some caveats, you can convert third-party TypeScript defintions to Scala [JS] API code to make calls with TypeScript-enabled libraries of strongly typed Scala code:

* ScalaJS code may depend only on cross-compiling other Scala-extended codes / libraries, but the 'normal' Scala can import any Java-compatible library.

UPDATE: Kotlin , Jetbrains (authors of various excellent IDEs including IntelliJ IDEA) is another JVM-oriented, concise, language, as well as support for cross-compiling Javascript. Theres also a one-click Java-to-Kotlin converter for Intellij and Eclipse.

Kotlin seems to be gaining momentum, is considered less complex than Scala, and is also used to develop Android and IOS applications ( RoboVM Studio ).

You can try Kotlin here .

+3
source share

TypeScript is another new language, and it quickly adds features. Even if such a transpiler existed, you would be in a very unfortunate situation, because the transpiler will always lag behind TypeScript capabilities.

A β€œsafer” approach would be to translate TypeScript into JavaScipt and translate into Java. This makes more sense to me because JavaScript is a standardized language that has reached some maturity, but would still be a bad idea. However, not even a transpiler for the last step.

Even if there were such transpilers, I definitely would not want to do this, because you were trapped. Transporters are by no means perfect, and the code will be more difficult for people to understand ( example ) and check.

Edit: Mozilla Rhino project can translate JavaScript into Java.

0
source share

I have a similar goal, and I'm going to try http://www.jsweet.org/ to translate java to javascript. Until now, some of them have been some limitations (without the support of the diamond operator, anonymous inner classes, etc.), but the advantages associated with the ability to write and maintain one code base instead of two are too great to ignore

0
source share

All Articles