Fork me on GitHub

Java loves TypeScript
Let’s focus on two brand new JSweet projects that will help making Java and TypeScript more interoperable.

  1. The online TypeScript to Java API translator
  2. J4TS: an Open Source project to implement Java APIs in TypeScript

(Java loves TypeScript :))

Background

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.

  • TypeScript well-formatted and readable JavaScript code, targeting ES3 to ES6 versions.
  • TypeScript implementations of class inheritance and enums, which are minimalist.
  • TypeScript support for modules (AMD, UMD, commonjs, …).
  • TypeScript support for typing (for instance union types).
  • TypeScript support for generating the d.ts files out of your Java program, so that it is easy to use a JSweet component from TypeScript!
  • And last but not least, DefinitelyTyped TypeScript libraries, packaged by JSweet as Maven artifacts (candies).

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 online TypeScript to Java API translator

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.

J4TS: an Open Source project that aims at implementing the Java APIs in TypeScript

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;

List l = new ArrayList();
l.add("a");

The beauty of that approach is that the JSweet transpiler compiles against TypeScript APIs by design. Thus, it will report TypeScript API issues as Java problems (use the current snapshot for that). For instance, if you try to access a method that is defined in Java, but not defined by J4TS, JSweet will report an error at compile time. Also, it will be extremely convenient to add support for Java APIs using this approach. Really, all you need to do is to fork the J4TS project, implement the missing Java part you need, and package/install the candy as explained in the project documentation. As easy as pie! Please PR your improvements 😀

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.

6 Comments

  • satguru says:

    This is great news !!
    JSweet keeps getting better :)

  • Maki says:

    Great news !

  • Artem Frolov says:

    That’s great!
    Is there a public repository with J4TS code?

  • Richard Quinn says:

    This sounds like a great addition. But I can’t get j4ts to work with the transpiler. I’ve added the j4ts dependency to the project’s pom.xml file as described above. When built with Eclipse (using the jsweet plugin), the simple java class below:

    package helloworld;
    import java.util.HashMap;
    public class testhash {
    public HashMap littlehash = new HashMap ();
    }

    always produces the error: “invalid access to JDK type ‘java.util.HashMap'” from JSweet, which is the same as if the j4ts dependency was not added. Has any one be using jsweet/j4ts with Eclipse.

    • Renaud Pawlak Renaud Pawlak says:

      The Eclipse plugin has not been updated with latest JSweet features yet. It will be done very soon. In the meantime, you should use the Maven plugin to have access to j4ts.

Leave a Reply

Your email address will not be published. Required fields are marked *


JSweet version 2 is now available!