-
Notifications
You must be signed in to change notification settings - Fork 242
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
Map from enum #421
Comments
Thanks for this suggestion. It looks good but I think type Test = "a" | "b";
const x: { [index in Test]: string } = {
a: "somethingA",
b: "somethingB",
} works well but const y: { [index in Test]: string } = {
a: "somethingA",
} doesn't work (missing property So if I understand it correctly we would need We would also need to ensure that this feature is not applied when |
Yes it does. This can be fixed by using |
- generating Mapped types like `{ [P in SomeEnum]?: SomeValue }` - Maps with enums involving numbers still tranformed to indexed types
Yes, you are right. It looks good so I implemented it. I did it myself because I wanted to go through different edge cases (mainly cases involving numbers in enums). When implementing this feature I also encountered and fixed some bugs. Could you please try it? |
Thanks! I found a small thing. First off, when you try to convert the following: interface TestInterface {
String test();
}
enum TestEnum implements TestInterface {
A,
B;
@Override
public String test() {
return name();
}
}
class Test<X extends Enum<X> & TestInterface> {
public List<Map<X, ?>> testMap;
}
class TestWithEnum extends Test<TestEnum> {
} you get export interface Test<X> {
testMap: { [index: string]: any }[];
}
export interface TestInterface {
}
export interface TestWithEnum extends Test<TestEnum> {
testMap: { [P in TestEnum]?: any }[];
}
export const enum TestEnum {
A = "A",
B = "B",
} Here you see that Some other issues I found (which are not directly to this issue I think):
abstract class GenericService<X, Y> {
@POST
open fun list(test: Map<X, Y>): String {
throw Exception()
}
}
@Path("")
class TestWithEnum : GenericService<String, String>() yields list(arg0: { [index: string]: Y }, options?: O): RestResponse<string> {
return this.httpClient.request({ method: "POST", url: uriEncoding``, data: arg0, options: options });
} Notice the unresolved
@Path("")
class TestWithEnum : GenericService<String, String>() {
override fun list(test: Map<String, String>): String {
println("Do something")
return super.list(test)
}
} there won't be any notion of the function whatoever, nothing is generated at all. I'm sure that in JAX-RS this is possible (see this):
In Spring MVC it generates duplicate methods: @RestController
public static class GreetingController extends TestController {
@Override
public Greeting greeting(String name) {
return super.greeting(name);
}
}
public static abstract class TestController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
} yields /**
* HTTP GET /greeting
* Java method: cz.habarta.typescript.generator.sample.spring.SpringTestApplication$GreetingController.greeting
*/
greeting$GET$greeting(queryParams?: { name?: string; }, options?: O): RestResponse<Greeting> {
return this.httpClient.request({ method: "GET", url: uriEncoding`greeting`, queryParams: queryParams, options: options });
}
/**
* HTTP GET /greeting
* Java method: cz.habarta.typescript.generator.sample.spring.SpringTestApplication$GreetingController.greeting
*/
greeting$GET$greeting(options?: O): RestResponse<Greeting> {
return this.httpClient.request({ method: "GET", url: uriEncoding`greeting`, options: options });
} |
That's a lot of feedback 😏
|
Thanks! In that case the generics resolving capabilities of typescript-generator are far superior to Spring's own :) I think to this issue only the first thing I mentioned is directly related (though probably very hard to solve nicely), and maybe the unresolved Annotation inheritance in JAX-RS might be worth another issue, but the Spring issues seem to be a lot of work in order to get to the same level as JAX-RS (and thus maybe are not worth it)? |
I wouldn't say it this way 😏. It is rather the way how typescript-generator uses Spring libraries. Regarding prioritization of issues I would say that Kotlin nullability could be very useful for people using Kotlin. And also compatibility with Java 13 will be important soon. |
I tried to rewrite your example with "unresolved |
Maybe something like #389 will be suitable here. |
I am seeing this commit. I dont' follow completely. However, I am looking for example 2 as illustrated by @RobbinBaauw . Will this work with the commit.
|
Yes that does work, it's my fault this issue became bigger than it was supposed to be 😅 |
Released in version 2.18.565. |
Fixed the following: - Generics resolving in both the return type and method arguments - PathVariable being string when used without value - Inheritance giving double methods (could be useful for JAX-Rs too)
* Fixed a bunch of Spring errors (#437 & #421) Fixed the following: - Generics resolving in both the return type and method arguments - PathVariable being string when used without value - Inheritance giving double methods (could be useful for JAX-Rs too) * Fixed checkstyle errors & added extra test case * Added support for bridged methods (used by Kotlin) * Fixed dangling semicolon
Currently, as far as I could find in the codebase, any map is turned into
{ [index: string]: ...}
, as visible here:typescript-generator/typescript-generator-core/src/main/java/cz/habarta/typescript/generator/DefaultTypeProcessor.java
Lines 84 to 87 in 48b30bd
But it would be nice if a
Map<SomeEnum, Any>
could get mapped into{ [index in keyof typeof SomeEnum]: ...}
. This would allow you to do typesafe accesses.Edit: This seems to be fully possible (using enums mapped to a custom string value, mapped to union type and mapped to key name).
Example 1:
maps to
Example 2:
maps to
Example 3:
maps to
In the future this could become even better: microsoft/TypeScript#26797.
Is this a feature that could be in the library @vojtechhabarta? I'm willing to implement it
The text was updated successfully, but these errors were encountered: