dzuvic / jtsgen Goto Github PK
View Code? Open in Web Editor NEWConvert Java Types to TypeScript
License: Other
Convert Java Types to TypeScript
License: Other
the name space of the generated types could be shortened, e.g. by mapping a specific top level package to a shorter one or even to the root name space. The priority of this feature depends on usability of the IDE regarding imports.
java.lang.NullPointerException: type
at java.util.Objects.requireNonNull(Objects.java:228)
at dz.jtsgen.processor.model.TSRegularMemberBuilder.<init>(TSRegularMemberBuilder.java:26)
at dz.jtsgen.processor.model.TSRegularMemberBuilder.of(TSRegularMemberBuilder.java:183)
at dz.jtsgen.processor.jtp.conv.JavaTypeElementExtractingVisitor.visitExecutable(JavaTypeElementExtractingVisitor.java:100)
at dz.jtsgen.processor.jtp.conv.JavaTypeElementExtractingVisitor.visitExecutable(JavaTypeElementExtractingVisitor.java:43)
Example:
`
declare module "exampleApi" {
export interface Order {
items: Array;
customer: Person;
}
}
`
and in package.json: instead typings use main
The easiest way for importing is to generate the output directly into the the client. Example:
import {Person} from "./api_types/exampleapi/example-api";
Note: this needs support for excluding a type by it's package, also. It's important because of the com.sun.misc.* Types
When using Kotlin and jtsgen classes with boolean members will not convert properly. Minimal example:
// Kotlin input
@TypeScript
class Foo(val bar: Boolean)
// Output
export interface Foo{}
Kotlin generates for bar
the getter getBar(). A workaround is to provide a function isBar() as getter. Then the output is generated right.
// Kotlin input
@TypeScript
class Foo(val bar: Boolean) { isBar() = bar }
//Output
export interface Foo {
bar: boolean
}
The name of this type misleading. It should be renamed to something like TypeMapInfo. It also represents a kind of type, but not a used one. During: #46
Private members with is-Getter are not generated in output file.
private boolean registered;
public boolean isRegistered() {
return registered;
}
Is it possible to add a "NO_MODULE" output type to the generator?
Inheritance is not working atm, could you please support it?
Only public types should be exported.
class attributes are exported as follows:
Getting jtsgen in a usable state user defined conversion has to be made possible:
since version 0.0.2 readonly
values are supported. But i think it's done in a weird way, for instance: take a bean with finals and a constructor: currently all members are declared read only, but that is not correct in any way, because it depends on how the instance is created. I am not sure if it could be possible to extract that information without type annotations, which are not supported by the regular annotation processing api. Therefore the user should be able to override read only types.
There a several use cases having classes instead of interfaces:
The mapping DSL has to be changed reflecting the default values of a type. The Default value should be changed by an TSOption Annotation
Since we are working with plain objects in JavaScript and not with class-instances we can't do typeof
checks. TypeScript has a feature called Type Guards that can help you with that.
Example:
class Person {
name: string;
age: number;
mail: string;
optionalVal?: any;
}
const person: Person = {
name: 'Can',
age: 6,
mail: '[email protected]'
};
const notAPerson = {
name: 'Can',
mail: '[email protected]'
};
console.log(typeof person, typeof person === typeof Person); // --> 'object', false :(
// with a 'typeguard' function
function instanceOfPerson(object: any): object is Person {
return 'name' in object
&& 'age' in object
&& 'mail' in object;
}
// usage:
console.log(typeof person, instanceOfPerson(person)); // --> 'object', true :)
console.log(typeof person, instanceOfPerson(notAPerson)); // --> 'object', false :)
https://codepen.io/anon/pen/weJbEr
So it'd be nice to have a setting || annotation to enable the generation for instanceOfX functions.
The javadoc string should be included in the TypeScript output
It seems, that declaring a type conversion from java.lang.Number -> number is ignored for it's sub types, e.g. BigDecimal.
Input:
public class Test<T> {
private List<T> someList = new ArrayList<>();
public List<T> getSomeList() { return this.someList;}
public void setSomeList(List<T> someList) {this.someList = someList;}
}
should result in:
export interface Test<T> {
someList: T[]; // or someList: Array<T>;
}
Relevant for this: http://central.sonatype.org/pages/ossrh-guide.html
I my opinion the name jtsgen
is not a good one to remember, because pronunciation is a bit awkward. The new name should be
The Type Variable is not generated:
Example:
interface Bla<T> {
val bla:Int
}
@TypeScript
interface BlaBla<T> : Bla<T> {
val blabla:Int
}
Leads to
export interface Bla<T> {
bla: number;
}
export interface BlaBla<T> extends Bla {
blabla: number;
}
This is a follow up of #23
The following will lead to generating a lot of not needed Java Types:
@TypeScript
public interface Issue_46_Wrong_Java_Types<T extends Integer> {
T getUpperBound();
}
According to that document jtsgen has to claim an unique namespace prior first deployment on maven central. Meaning: The package name has to correspond a controlled / existing domain name.
When using Jackson and the @JsonProperty annotation to rename properties, jtsgen generates interfaces with wrong name.
Example:
@Typescript
data class Foo(
@JsonProperty("foo_bar") @get:JsonProperty("foo_bar") val bar: String
)
generates: interface Foo { bar: string }
warning: Implicitly compiled files were not subject to annotation processing.
This is a followup of #28. Direct only type mapping should also be supported. But maybe this is not needed. At first a proper use case should be defined for this kind of mapping.
Types that are not defined in it's own compilation unit are not extracted without a reference to an annotated class. Therefore
TSModule
annotationthe runtime scope of the generated maven pom is wrong:
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>2.5.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>builder</artifactId>
<version>2.5.5</version>
<scope>runtime</scope>
</dependency>
The following two mapping should be done i another way:
java.util.Collection<T>
and its sub types should be mapped to T[]
java.util.map<K,V>
should be mapped to list of a type {[key: K]: V}
For this the mapping DSL should be extended to the the following:
->
For example, when defining a mapping like this:
java.uti.Map<K,V> -> {[key: `K`]: `V`}
the an HashMap<String,Integer>
will be converted to {[kex: string]: number}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.