hilla icon indicating copy to clipboard operation
hilla copied to clipboard

add a @TypeScriptType annotation to configure the generated TypeScript types

Open vlukashov opened this issue 5 years ago • 3 comments

When defining Java types that are used from TypeScript, I want have a @TypeScriptType annotation that I can put on a Java property to define its type in the generated TypeScript entity, so that I can avoid downcasting in cases when I know the how this property is represented in JSON / JavaScript at the run time.

The new annotation should work with all ways of defining a TypeScript property: fields, getters and setters, getters and setters annotated with @JsonProperty.

For example, as of Spring Boot 2.2 / Jackson 2.10 the UUID Java type has a default serializer that converts UUIDs to strings. In this case it would be more convenient to map Java UUID to TS string rather than to any, which is the default for non-primitive types.

Expected:

public class Request {
    @TypeScriptType("string")
    private UUID uuid1;
    private UUID uuid2;
}
export default interface Request {
  uuid1: string;
  uuid2: any;
}

vlukashov avatar Apr 15 '20 09:04 vlukashov

Some variations to consider

  • Could this be per type instead of per field? It seems reasonable that I want to represent all properties of type UUID as a TS string rather than only selected ones. From that point if view, a more convenient (but not as flexible) approach would be to globally define the mapping instead of repeating the same definition for each property.
  • Should we assume that the application developer can conveniently add annotations on the actual type. Another alternative is to (optionally) support a qualified definition form the parent context, i.e. a parent bean or for an endpoint parameter or return value. This could thus be like this for an endpoint parameter:
public Response handleRequest(@TypeScriptType(value = "string", property = "uuid1") Request request) {
 ...
}

Legioth avatar Apr 15 '20 09:04 Legioth

This issue here would offer a possibility to work around the existing limitation ASAP, as well as a mechanism for fine tuning the defaults in the future. I do not see it as sufficient for covering how Java/TypeScript type mapping is defined and configured.

Could this be per type instead of per field?

Indeed, having to put the same mapping in every type that has a UUID property would be inconvenient. However, at the moment Vaadin TypeScript generator does not have any other configuration than annotations that it reads from the Java source code. Since it runs at the build time, the configuration cannot be expressed in imperatively inside the program source code. It has to be done either with more annotations, or in a separate config file. The proposal described in this issue does not define how that would be done.

Should we assume that the application developer can conveniently add annotations on the actual type. Another alternative is to (optionally) support a qualified definition form the parent context, i.e. a parent bean or for an endpoint parameter or return value.

Indeed, annotations are limited in that they can only be placed on the types the developer has control over. For 3rd party Java types the proposal described in this issue does not define how that would be done.

vlukashov avatar Apr 15 '20 10:04 vlukashov

Another use case where something like this would be useful: restrict the possible values of a string-typed property:

I want to configure the Fusion TS type generator to produce

export default interface GridColumnConfigurator extends AbstractEntity {
  direction?: 'asc' | 'desc';
}

instead of

export default interface GridColumnConfigurator extends AbstractEntity {
  direction?: string;
}

vlukashov avatar Dec 28 '20 10:12 vlukashov