Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

This project generates TypeScript code from given Java Classes

Output format

Java classes are converted to TypeScript interfaces.

Module (generated output) format

TypeScript has concept of internal and external modules, that have slightly different format (see the test output for internal vs external module format). By default internal module format is used (that adds one line before and after content of external module format).

External module format can be used by using different ModuleWriter (ExternalModuleFormatWriter - see the test for an example)

Ignored methods

When generating TypeScript from Java classes (actually when Java classes are analysed), some methods are excluded:

  • Non-public methods
  • Methods annotated with @java.beans.Transient
  • Java Bean property getters/setters are excluded even if field doesn't exist with exact name (field is generated based on corresponding Java Bean property name instead)
  • Methods ignored by the configuration:
		Configuration conf = new Configuration();
		conf.addIngoredMethod("blacklistedMethod");

See the tests for excluded methods for details.

Note: You can also extend the configuration and overwrite isIgnoredMethod method to programmatically make the decision (most likely based on method signature: parameters, return type, declaring class, annotations, ...)

Enums

Java enums are converted to TypeScript enums by default, but TypeSafe enum pattern can be used to force generating classes instead of enums (see the description of the issue. Example output from test that turns on this preference using

mWriter.preferences.useEnumPattern();

Mapping specific java classes to custom TypeScript types

There are scenarios when You might want to use different TypeScript type instead of specific Java Type. There are several options for doing this depending how You want it to be done:

Renaming Type and emitting it to the output

Logic, that determines the name of TypeScript type based on Java class is implemented using TSTypeNamingStrategy interface. Currently there are two implementations provided out of the box by this library:

  1. SimpleJacksonTSTypeNamingStrategy - The default, uses Java class name for TypeScript type, unless class has annotation, that specifies custom name (see bellow).
  2. WithEnclosingClassTSTypeNamingStrategy - extends SimpleJacksonTSTypeNamingStrategy to include enclosing class name as a prefix (javaClass.getName() without package ).
Renaming Type using annotation on the Java class (SimpleJacksonTSTypeNamingStrategy)

You can use @com.fasterxml.jackson.annotation.JsonTypeName("ChangedEnumName") annotation on the Java type to use different name in TypeScript output (interface/enum with different name is also generated to the output). See the example from the test.

Renaming Types using custom (re)naming strategy

To tweak naming TypeScript types for Your specific needs, You can provide an implementation of TSTypeNamingStrategy, such as SimpleJacksonTSTypeNamingStrategy (or write Your own) to Configuration.setNamingStrategy(namingStrategy).

See the test TypeRenamingWithEnclosingClassTest and the expected output of the test.

Using different type, not emitting it to the output

One common use-case, could be for instance Joda or Java8 LocalDate or Date or Calendar to TypeScript/JavaScript Date. See the test source and output as an example.

Mapping Java type to TypeScript type is done using:

new Configuration().addType(CustomDate.class, DateType.getInstance())

where DateType class specifies the expected output type name.