Skip to content

Mapping generic classes using customTypeMappings parameter #384

Closed
@vojtechhabarta

Description

@vojtechhabarta

It is possible to configure how some Java class is mapped to TypeScript. For example org.joda.time.LocalDateTime Java class can be mapped to string in TypeScript. For this purpose there is customTypeMappings parameter which is list where each item specifies one mapping, for example like this: org.joda.time.LocalDateTime:string.

Mapping generic Java classes to TypeScript non-generic types

Originally when Java class was generic (for example com.package.IdRepresentation<T>) it could have been mapped to non-generic type like string using com.package.IdRepresentation:string mapping.

Mapping generic Java classes to TypeScript generic types

To support custom mapping of container types like lists and maps typescript-generator from version 2.10.466 mapped Java generic type parameters to TypeScript generic types. For example mapping Java class ListWrapper<T> to TypeScript type ListWrapper<T> could have been configured using com.package.ListWrapper:ListWrapper custom mapping. Unfortunately this feature broke previous possibility to map generic Java class to non-generic TypeScript type.

Solution

To distinguish between these two cases new syntax is needed. But it is not clear which behavior should be preserved and which would use new syntax. To avoid ambiguity new syntax is required for all generic Java classes. This new syntax supports both mentioned cases and also allows new possibilities. Here are some examples:

com.package.IdRepresentation<T1>:string                     # first case
com.package.MyGenericClass<T1,T2>:TsGenericType2<T1,T2>     # second case
com.package.MyGenericClass<T1,T2>:TsGenericType2<T2,T1>     # new
com.package.MyGenericClass<T1,T2>:TsGenericType1<T2>        # new
com.package.MyGenericClass<T1,T2>:TsGenericType3<T1,T2,T1>  # new
com.package.Wrapper<T>:T                                    # new ("unwrap")

To prevent character escaping in Maven pom.xml files it is also possible to use [] characters instead of <>. Here is Maven example:

<configuration>
    <customTypeMappings>
        <mapping>com.package.IdRepresentation[T1]:string</mapping>
        <mapping>com.package.MyGenericClass[T1,T2]:TsGenericType2[T1,T2]</mapping>
        <mapping>com.package.Wrapper[T]:T</mapping>
    </customTypeMappings>
</configuration>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions