Template engine for TypeScript


License

License

GroupId

GroupId

io.github.daniloarcidiacono
ArtifactId

ArtifactId

typescript-template
Last Version

Last Version

0.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Template engine for TypeScript
Template engine for TypeScript
Project URL

Project URL

http://github.com/daniloarcidiacono/typescript-template
Source Code Management

Source Code Management

https://github.com/daniloarcidiacono/typescript-template.git

Download typescript-template

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.daniloarcidiacono/typescript-template/ -->
<dependency>
    <groupId>io.github.daniloarcidiacono</groupId>
    <artifactId>typescript-template</artifactId>
    <version>0.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.daniloarcidiacono/typescript-template/
implementation 'io.github.daniloarcidiacono:typescript-template:0.0.1'
// https://jarcasting.com/artifacts/io.github.daniloarcidiacono/typescript-template/
implementation ("io.github.daniloarcidiacono:typescript-template:0.0.1")
'io.github.daniloarcidiacono:typescript-template:jar:0.0.1'
<dependency org="io.github.daniloarcidiacono" name="typescript-template" rev="0.0.1">
  <artifact name="typescript-template" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.daniloarcidiacono', module='typescript-template', version='0.0.1')
)
libraryDependencies += "io.github.daniloarcidiacono" % "typescript-template" % "0.0.1"
[io.github.daniloarcidiacono/typescript-template "0.0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
io.github.daniloarcidiacono : commons-lang jar 0.1.0

test (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar

Project Modules

There are no modules declared in this project.

Maven Central codecov CircleCI License: MIT

Typescript Template

This is a library to write formatted TypeScript code using a fluent API.

Here is a simple initialization and usage example:

public class Main {
    public static void main(String[] args) {
        final TypescriptSource source = new TypescriptSource()
            .statement(
                new TypescriptNamespaceImport("../address.d.ts")
                    .selector(new TypescriptImportSelector("Address")),
                new TypescriptInterface(
                    "Person",
                    new TypescriptInterfaceType("Object")
                )
                .setComments(new TypescriptComments("Represents a person."))
                .field(new TypescriptField("name", TypescriptStringType.INSTANCE, TypescriptField.MANDATORY))
                .field(new TypescriptField("age", TypescriptNumberType.INSTANCE, TypescriptField.MANDATORY))
                .field(
                    new TypescriptField("address", new TypescriptInterfaceType("Address"))
                        .setComments(new TypescriptComments("Address of the person.", "(optional field)"))
                )
            );

        // Pretty print
        System.out.println(source.render());
    }
}

outputs:

import { Address } from '../address.d.ts';
// Represents a person.
export interface Person extends Object {
	name: string;
	age: number;

	/**
	 * Address of the person.
	 * (optional field)
	 */
	address?: Address;
}

Supported language constructs

  • Primitive types (undefined, null, any, void, boolean, number, string);
  • Arrays;
  • Dictionaries (e.g. { [id: string]: IPerson; });
  • Generics;
  • Union types (e.g. string | number);
  • Interface declarations, including inheritance and generic arguments;
  • Enum declarations;
  • Type aliases declarations;
  • Imports;
  • Comments (single and multiline);

Some basic static analysis can be performed by visiting the AST:

    public class Main {
        public static void main(String[] args) {

            final TypescriptSource source = ...;
            
            // Print dependencies
            System.out.println("Dependencies:");
            source.accept(renderable -> {
                if (renderable instanceof TypescriptInterfaceType) {
                    System.out.println(((TypescriptInterfaceType) renderable).getIdentifier());
                }
            });
        }
    } 

yields:

Dependencies:
Object
Address

The latest version is available on Maven Central.

Versions

Version
0.0.1