This project generates TypeScript code from given Java Classes
Java classes are converted to TypeScript interfaces.
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)
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
isIgnoredMethodmethod to programmatically make the decision (most likely based on method signature: parameters, return type, declaring class, annotations, ...)
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();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:
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).
https://github.com/raphaeljolivet/java2typescript/blob/master/java2typescript-jackson/src/test/java/java2typescript/jackson/module/DefinitionGeneratorTest.java#L37
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.