Skip to content

Commit 01635f2

Browse files
Fix for generic parameter in method declaration
1 parent 982ab76 commit 01635f2

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/DefaultTypeProcessor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public Result processType(Type javaType, Context context) {
103103
}
104104
if (javaType instanceof TypeVariable) {
105105
final TypeVariable<?> typeVariable = (TypeVariable<?>) javaType;
106+
if (typeVariable.getGenericDeclaration() instanceof Method) {
107+
// example method: public <T extends Number> T getData();
108+
return context.processType(typeVariable.getBounds()[0]);
109+
}
106110
return new Result(new TsType.GenericVariableType(typeVariable.getName()));
107111
}
108112
if (javaType instanceof WildcardType) {
@@ -138,6 +142,7 @@ private static Map<Type, TsType> getKnownTypes() {
138142
knownTypes.put(String.class, TsType.String);
139143
knownTypes.put(void.class, TsType.Void);
140144
knownTypes.put(Void.class, TsType.Void);
145+
knownTypes.put(Number.class, TsType.Number);
141146
// other java packages
142147
knownTypes.put(BigDecimal.class, TsType.Number);
143148
knownTypes.put(BigInteger.class, TsType.Number);

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/GenericsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ public void testGenericArray() {
125125
assertEquals(expected, output.trim());
126126
}
127127

128+
@Test
129+
public void testArbitraryGenericParameter() {
130+
final Settings settings = TestUtils.settings();
131+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(ExecutionResult.class));
132+
final String expected =
133+
"interface ExecutionResult {\n" +
134+
" data: number;\n" +
135+
"}";
136+
assertEquals(expected, output.trim());
137+
}
138+
128139
class A<U,V> {
129140
public A<String, String> x;
130141
public A<A<String, B>, List<String>> y;
@@ -173,4 +184,8 @@ class TableGA<T> {
173184
public T[] rows;
174185
}
175186

187+
interface ExecutionResult {
188+
public <T extends Number> T getData();
189+
}
190+
176191
}

0 commit comments

Comments
 (0)