-
Notifications
You must be signed in to change notification settings - Fork 237
Type Mapping
this page describes how typescript-generator maps Java types to TypeScript types.
For each input Java class or interface typescript-generator outputs corresponding TypeScript interface (can also be set to generate classes). Name of generated interface is based on class simple name and can be customized using several parameters:
- it is possible to change prefixes or suffixes of all names at once (parameters
removeTypeNamePrefix
,removeTypeNameSuffix
,addTypeNamePrefix
,addTypeNameSuffix
) - it is also possible to specify new names for particular classes using
customTypeNaming
parameter (see example bellow)
Each interface contains properties which are obtained using JSON library. Library is set using jsonLibrary
parameter.
Typescript-generator recognizes following Java types:
Java type | TypeScript type |
---|---|
Object | any |
byte, Byte, short, Short, int, Integer, long, Long | number |
float, Float, double, Double | number |
boolean, Boolean | boolean |
char, Character | string |
String | string |
BigDecimal, BigInteger | number |
Date | Date, string or number depending on mapDate parameter |
UUID | string |
T[] | T[] |
Collection<T> | T[] |
Map<String, T> | { [index: string]: T } |
Enum | string literal union (for example: "Left" | "Right") or other possibilities depending on mapEnum parameter |
It is possible to customize type mapping using customTypeMappings
parameter.
Each item in this list maps Java type to TypeScript type name. TypeScript type can be:
- built-in TypeScript type (for example
string
) - generated type (for example
MyClass
) - type from some existing or custom library (for example
joda.Money
)
Library can be added using importDeclarations
parameter (or referencedFiles
when not using modules).
Please note that there are two similarly named parameters:
customTypeNaming
andcustomTypeMappings
. First one specifies how generated interfaces (or classes) are named while second specifies how Java types are mapped to existing types.
Maven pom.xml
<plugin>
<groupId>cz.habarta.typescript-generator</groupId>
<artifactId>typescript-generator-maven-plugin</artifactId>
<version>FILL_LATEST_RELEASE_HERE</version>
<configuration>
<!-- common settings -->
</configuration>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<jsonLibrary>jackson2</jsonLibrary>
<classes>
<class>cz.habarta.typescript.generator.sample.Person</class>
</classes>
<customTypeNaming>
<naming>org.company.Product$Type:ProductType</naming>
</customTypeNaming>
<customTypeMappings>
<mapping>org.joda.time.DateTime:string</mapping>
<mapping>org.joda.money.Money:joda.Money</mapping>
</customTypeMappings>
<importDeclarations>
<import>import * as joda from "../src/ts/joda.d.ts"</import>
</importDeclarations>
<outputFile>target/sample.d.ts</outputFile>
<outputKind>module</outputKind>
</configuration>
</execution>
</executions>
</plugin>
Note that
<configuration>
element is sometimes directly under<plugin>
element and sometimes under<execution>
element depending on usage.
Person.java
public class Person {
public String name;
public org.joda.time.DateTime birthday;
public org.joda.money.Money cash;
}
joda.d.ts
export interface Money {
amount: number;
currencyCode: string;
}
Generated output file sample.d.ts
import * as joda from "../src/ts/joda.d.ts";
export interface Person {
name: string;
birthday: string;
cash: joda.Money;
}