A Gradle plugin that generates Typescript interfaces from Kotlin/Java classes
MIT License
Another Typescript Generator is a Gradle plugin that generates Typescript interfaces from Kotlin/Java classes. Each class generates a new file to prevent collision.
classes
command before.class Company(
id: String? = null,
val name: String,
val money: BigDecimal,
val imageUrl: String,
) : DatabaseEntity(
id,
) {
@ManyToOne(fetch = FetchType.LAZY)
var owner: Player? = null
}
will generate
import {DatabaseEntity} from './database-entity';
import {Player} from './player';
export interface Company extends DatabaseEntity {
imageUrl: string;
money: number;
name: string;
owner?: Player | null;
id?: string | null;
}
Add id("io.github.ynixt.another-typescript-generator") version "1.1.0"
at plugins section
plugins {
id("io.github.ynixt.another-typescript-generator") version "1.1.0"
}
Kotlin type | Typescript type | Note |
---|---|---|
LocalDate, LocalDateTime, ZonedDateTime, Date | string | Can be easily modified using the configuration parameter "mapDate" |
String | string | |
Byte, Short, Int, Long, Float, Double, BigDecimal, BigInteger | number | |
Boolean | boolean | |
Collection | Array<T> | |
Map<K, T> | { [key: string]: T } | |
any other type | any |
You can change or add any mapping using the configuration parameter customTypes
.
Modify the task named generateTypescriptInterfaces
. Here's a simple example:
tasks.named<GenerateTypescriptInterfacesTask>("generateTypescriptInterfaces") {
outputPath = "./src/models/generated"
classPackages =
listOf(
"io.github.ynixt.example",
)
}
Path, relative or absolute, that will placed the generated *.ts files.
outputPath = "./src/models/generated"
A permissive list of packages that contain the classes that will be used to generate the files.
Note: A class not listed here can be read if a listed class has a reference for it.
classPackages = listOf(
"io.github.ynixt.example",
)
A prohibitive list of packages. It takes priority over the permissive list.
Note: A class listed here can be read if a not listed class has a reference for it. If you really don't want this class use "ignoredClasses" param
excludeClassPackages = listOf(
"io.github.ynixt.example",
)
A list of classes that will not be read, no matter what. If there is any reference to it, this reference will be replaced by any
.
ignoredClasses = listOf(
"io.github.ynixt.example.Book",
)
A map of fields of classes that should be ignored.
ignoredClasses = mutableMapOf(
"io.github.ynixt.example.Person" to setOf("name", "age")
)
Defines which Typescript type should be used when a date type field is encountered (Date, LocalDate, LocalDateTime, ZonedDateTime).
MapDateOption | Typescript type | Default |
---|---|---|
AS_STRING | string | X |
AS_LUXON | DateTime | |
AS_MOMENT | moment.Moment | |
AS_DATE | Date | |
AS_NUMBER | number |
mapDate = MapDateOption.AS_LUXON
Add or replace the mapping between Typescript and Kotlin.
customTypes = listOf(
CustomType(
kotlin = AbsoluteKotlinType(String::class),
typescript = AbsoluteTypescriptType(name = "any")
),
CustomType(
kotlin = AbsoluteKotlinTypeString("io.github.ynixt.entities.Book"),
typescript = AbsoluteTypescriptType(name = "string", ignoreGenerics = true)
)
)