cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 160 forks source link

TypeScript strict mode compatibility #554

Open stahlbauer opened 4 years ago

stahlbauer commented 4 years ago

I would like to use the TypeScript compiler (compiling von Java to TypeScript) with the option --strict. Nevertheless, the generated code is not yet fully strict, while this would be possible.

At the moment, JSweet generates code like the following:

class Test {
      p1: Foo;
      public constructor(p: Foo) {
          if (!this.p1) this.p1 = null;
      }
 }

This results in several compiler errors if --strict is enabled:

error TS2565: Property 'p1' is used before being assigned.

         if (!this.p1) this.p1 = null;
                    ~~
error TS2322: Type 'null' is not assignable to type 'Foo'.

         if (!this.p1) this.p1 = null;

The relevant code in Jsweet is in the class Java2TypeScriptTranslator at line 2649.

lgrignon commented 4 years ago

Hello, there is currently no plan to support it but it would be a nice addition to JSweet! That being said, I think it would not be so easy to make generated TS code compatible with strict mode. If you want to give a try, all PR are welcome. I recommend that you start from Java11 branch if you intend to do so because it changed a lot of things. Thanks anyway for your suggestion

stahlbauer commented 4 years ago

Thank you for the response.

lgrignon commented 3 years ago

I reopen this ticket because this feature would be useful, if community wants to contribute to it some day