Let’s focus on two brand new JSweet projects that will help making Java and TypeScript more interoperable.
(Java loves TypeScript :))
From day one, the JSweet transpiler (Java to JavaScript) is implemented as an additional transpilation layer on the top of TypeScript. This architectural choice was firstly made to ease the transpilation implementation, since Java and TypeScript belong to the same family of object-oriented languages. Along the way, it became clear that this architectural choice was a good idea, since JSweet can benefit from all the experience of the TypeScript community and the excellent features and design choices available in TypeScript. For example, JSweet takes advantage of the following features.
d.ts
files out of your Java program, so that it is easy to use a JSweet component from TypeScript!Additionally, with JSweet, it is possible to share Java beans between a Java server and a TypeScript frontend (use the @Interface(SHARED)
annotation on your beans and JSweet will generate the corresponding TypeScript interfaces).
So far so good. But JSweet now strives for even more Java/TypeScript interoperability, by making available two new projects!
The first project is to make available to anyone a web service for translating any TypeScript d.ts
definition file to Java. The beta version of this service is available and you can try it live! All you need to do it to zip your TypeScript d.ts
as explained in the small documentation and you are ready to go. The service will return a zip containing the Java source code of the translated TypeScript API.
The idea behind this is that programmers won’t get stuck waiting for PRs to go through DefinitelyTyped. They can improve their favorite library definition and translate it right away to Java, and use it with JSweet (see how to create a JSweet candy here). Programmers can of course use the service for translating their own JavaScript libraries/components.
The goal of J4TS is to have a TypeScript implementation of the Java APIs. This project can be useful to Java programmers wanting to switch to TypeScript without leaving their comfort zone completely. In fact, most JavaScript core APIs have been designed for JavaScript and with a JavaScript state of mind. So, it is good to be able to count on good old Java APIs, which have been around for so long that everybody knows them. For example, with J4TS you can write the following TypeScript code:
import List = java.util.List; import ArrayList = java.util.ArrayList; var l: List= new ArrayList (); l.add("a");
Looks familiar?
Besides, the other purpose of J4TS will be to serve transpilers such as JSweet, by having a well-typed runtime for emulating the Java APIs. Having this runtime written in TypeScript is especially interesting for JSweet because it is really easy to package it as a candy, so that JSweet programmers can access the Java APIs in JSweet!
Accessing Java APIs in JSweet gets as simple as declaring a new dependency in your pom.xml
.
<dependency> <groupId>org.jsweet.candies</groupId> <artifactId>j4ts</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
Then, you can use all the APIs declared/implemented by J4TS and JSweet will magically accept them!
import java.util.List; import java.util.ArrayList; Listl = new ArrayList (); l.add("a");
The beauty of that approach is that the JSweet transpiler compiles against TypeScript APIs
That will be it. I hope that you will enjoy it! Please remember that these two projects are beta and we are hoping for feedback 😉 Note that J4TS will only work with the latest JSweet transpiler snapshot.