-
Notifications
You must be signed in to change notification settings - Fork 239
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. 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
- it is also possible to specify new names for particular classes using
customTypeNaming
parameter
Each interface contains properties which are obtained using JSON library. Library is set using jsonLibrary
parameter. Supported is jackson1
and jackson2
.
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") |
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 referencedFiles
or importDeclarations
parameters.
pom.xml
<plugin>
<groupId>cz.habarta.typescript-generator</groupId>
<artifactId>typescript-generator-maven-plugin</artifactId>
<version>1.7-SNAPSHOT</version>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<jsonLibrary>jackson2</jsonLibrary>
<classes>
<class>cz.habarta.typescript.generator.sample.Person</class>
</classes>
<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>
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;
}