Skip to content

Type Mapping

Vojtěch Habarta edited this page May 3, 2016 · 2 revisions

this page describes how typescript-generator maps Java types to TypeScript types.

Classes and interfaces

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.

Default type mapping

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")

Customizations

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.

Customization Example

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;
}
Clone this wiki locally