Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type mapping with generic #197

Closed
aspirisen opened this issue Nov 28, 2017 · 8 comments
Closed

Type mapping with generic #197

aspirisen opened this issue Nov 28, 2017 · 8 comments

Comments

@aspirisen
Copy link

Hello, i have some difficulties with the following situation, I have error Multiple classes are mapped to the same name. and I try to use customTypeMappings but the issue is that theses classes has Generic type. How can I set generics in customTypeMappings?

@vojtechhabarta
Copy link
Owner

Hello,
you can use customTypeNaming parameter (not customTypeMappings) to specify new names for your conflicting classes or you can use excludeClasses to exclude classes you don't need.
See Configuration parameters page for description.

@aspirisen
Copy link
Author

aspirisen commented Nov 29, 2017

Yes, I tried to use customTypeNaming and it almost hellped.
In java I have to different classes ListWrapper that have the same fields and the same types, so in TypeScript output I could use the same interface, but when I set the typescript name it throws the error.

i.e. pseudocode

package a.b.c;

class ListWrapper<T> {
  publuc id: string;
  publuc values: List<T>;
}

package a.b.d;

class ListWrapper<T> {
  publuc id: string;
  publuc values: List<T>;
}

and in cofig I set a.b.d.ListWrapper:ListWrapper but it causes a error. So i set the following a.b.c.ListWrapper:ListWrapperAnother and in TypeScript output I have the same interfaces that can confuse developers, it would be good if it is possible to set that for the specific Java class you can use the following TypeScript interface without checking. I hope I could explain.

@vojtechhabarta
Copy link
Owner

Mapping two Java classes to one TypeScript interface (even using customTypeNaming) is not possible
and original goal of customTypeMappings is to map some Java type to some external TypeScript type (and you are right it doesn't work with generics).

However TypeScript has structural typing so generating two identical interfaces should not be a problem:

interface ListWrapper1<T> {
    id: string;
    values: T[];
}

interface ListWrapper2<T> {
    id: string;
    values: T[];
}

const lw1: ListWrapper1<string> = {
    id: "id1",
    values: ["a"]
};

const lw2: ListWrapper2<string> = lw1;

@aspirisen
Copy link
Author

Yes, it works in TypeScript and it doesn't show an error, but I suppose that it will break a bit find usages and references as it will be two different interfaces

@vojtechhabarta
Copy link
Owner

I agree it's not optimal. Right solution would be to fix it in Java but I understand that it might not be always possible. Maybe I could make customTypeMappings work with generics sometime in the future.

@aspirisen
Copy link
Author

Ok, thanks

@vojtechhabarta
Copy link
Owner

Finally I returned to this issue and it is now possible to use generics in customTypeMappings parameter.
Released in v2.10.466.

@vojtechhabarta
Copy link
Owner

Unfortunately mentioned change in v2.10.466 broke mapping of generic classes to TypeScript non-generic types like string. So in 2.16.538 release I changed customTypeMappings parameter format (#384) and it is now needed to specify generic classes included their generic parameters, for example Maven configuration:

<customTypeMappings>
    <mapping>ListWrapper[T]:TsListWrapper[T]</mapping>
</customTypeMappings>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants