Skip to content

Tool for generating TypeScript definition files (.d.ts) from Java JSON classes.

License

Notifications You must be signed in to change notification settings

thamacher/typescript-generator

 
 

Repository files navigation

typescript-generator

typescript-generator is a tool for generating TypeScript definition files (.d.ts) from Java JSON classes. If you have REST service written in Java using object to JSON mapping you can use typescript-generator to generate TypeScript interfaces from Java classes.

For example for this Java class:

public class Person {
    public String name;
    public int age;
    public boolean hasChildren;
    public List<String> tags;
    public Map<String, String> emails;
}

typescript-generator outputs this TypeScript interface:

interface Person {
    name: string;
    age: number;
    hasChildren: boolean;
    tags: string[];
    emails: { [index: string]: string };
}

Supported types include:

  • all Java primitive types with their corresponding wrappers (for example int and Integer, boolean and Boolean, etc.)
  • String
  • Date
  • enum
  • array
  • List and Map (including derived interfaces and implementation classes)

Maven

In Maven build you can use typescript-generator-maven-plugin like this:

<plugin>
    <groupId>cz.habarta.typescript-generator</groupId>
    <artifactId>typescript-generator-maven-plugin</artifactId>
    <version>x.y.z</version>
    <executions>
        <execution>
            <id>generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <classes>
                    <class>cz.habarta.typescript.generator.Person</class>
                </classes>
                <namespace>Rest</namespace>
                <outputFile>target/rest.d.ts</outputFile>
            </configuration>
        </execution>
    </executions>
</plugin>

More complete sample can be found here. Detailed description how to configure typescript-generator-maven-plugin is on generated site.

Gradle

In Gradle build you can use cz.habarta.typescript-generator plugin like this:

apply plugin: 'cz.habarta.typescript-generator'
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath group: 'cz.habarta.typescript-generator', name: 'typescript-generator-gradle-plugin', version: 'x.y.z'
    }
}
generateTypeScript {
    outputFile = 'build/sample.d.ts'
    classes = [
        'cz.habarta.typescript.generator.sample.Person'
    ]
    jsonLibrary = 'jackson2'
    namespace = 'Rest';
//    module = 'my-module'
//    declarePropertiesAsOptional = false
//    removeTypeNameSuffix = 'Json'
//    mapDate = 'asNumber'
}

More complete sample can be found here. Gradle plugin has the same features as Maven plugin, for detailed description see Maven generated site.

Direct invocation

If you do not use Maven or Gradle you can invoke typescript-generator directly using TypeScriptGenerator.generateTypeScript() method.

Download

Releases are available from Maven Central Repository. Search for dependency information for your build tool or download typescript-generator-core directly.

Wiki

For more detailed description of some topics see Wiki pages.

Architecture

TypeScriptGenerator has 3 main parts (ModelParser, ModelCompiler and Emitter) which work together to produce TypeScript declarations for specified Java classes.

           (Model)            (TsModel)
ModelParser  ==>  ModelCompiler  ==>  Emitter
         |         | 
         V         V
        TypeProcessor
  • ModelParser reads Java JSON classes and their properties using Java reflections and creates Model. It uses TypeProcessors for finding used classes. For example if property type is List<Person> it discovers that Person class should be also parsed. ModelParsers are specific for each JSON library (for example Jackson2Parser).
  • ModelCompiler transforms Java model to TypeScript model (Model class to TsModel class). It uses TypeProcessors for mapping Java types to TypeScript types (for example for int returns number).
  • Emitter takes TsModel and produces TypeScript declaration file.

Contributing

  • this project targets Java 7
  • keep pull requests small and focused (10 tips for better Pull Requests)
  • do not add dependencies unless previously discussed in issue

Code formatting

  • use 4 spaces for indentation in Java files
  • sort java imports alphabetically, you can use wildcards
  • please do not reformat whole files in IDE

About

Tool for generating TypeScript definition files (.d.ts) from Java JSON classes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 97.3%
  • PowerShell 2.1%
  • TypeScript 0.6%