-
Notifications
You must be signed in to change notification settings - Fork 6k
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
Improve TypeScript Template #1334
Comments
@aersamkull thanks for offering contribution to the TypeScript generator. Would you please elaborate on your third point about using interfaces instead of models? For best practices, I assume you're referring to http://definitelytyped.org/guides/best-practices.html. Later we want to support inheritance in all generators: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#composition-and-inheritance-polymorphism |
I just looked at Rules from Tslint: https://github.com/palantir/tslint The namespace keyword is a replacement for the module keyword: microsoft/TypeScript#2923 module is no more encouraged On classes: interface a_i { b: string};
class a_c {b: string;}
let v1 : a_i = {"b": "asdf"}; //Works
let v2: a_c = {"b": "asdf"}; //Does not Work |
Hi @aersamkull @wing328, I would like to come back to this issue. At the moment the Angular Codegen template generate interfaces for the model. To be able to use interfaces instead of classes, you need to use the Elvis operator in the Angular HTML files. Let's say we have a
We have to make sure
But for forms, this is not possible. Elvis operator is not possible in two-way binding. But using class, is fine. So when Codegen generates interfaces, I always change them to classes. If you look at the official Angular documentation, they use classes for the model, not interfaces. And in my understanding, Codegen used to use classes before. Are you sure about interfaces ? If you want to keep interfaces, what about generating at least one implementation ( |
@agoncal may I know if you've time to contribute a PR to use class instead of interface? If yes, I can show you some good starting point. Please base the fix on 2.3.0 branch, which contains breaking changes. |
@wing328 I'm ok to contribute.... but I'm totally new in contributing to Swagger Codegen. So I wouldn't mind a bit of help with some starting points. |
@agoncal I think a good starting point is https://github.com/swagger-api/swagger-codegen/blob/2.3.0/modules/swagger-codegen/src/main/resources/typescript-angular/model.mustache#L11 (ts-angular model template in 2.3.0 branch) After you modify it, you can run the following commands to regenerate the Petstore sample.
|
@agoncal may I know if you need help with the PR/enhancement? |
I'm sorry @wing328 I won't have time to work on this for a few weeks. Hopefully I'll be available from mid-may. It's in my TODO list ;o) |
@agoncal no problem. Let us know if you need any help with the PR/enhancement. |
I'm using this generation in one of my projects and my app won't compile due to a few simple typescript errors generated by the code generator. I'm looking into it right now. @mhardorf and @tandrup could one of you guys perhaps explain why the generated models are interfaces? Would it not make more sense to make those classes? I'm up for changing to classes because in my experience an interface doesn't contain attributes. But maybe there is something I'm not seeing right now. Repo so far: https://github.com/iain17/swagger-codegen/tree/improvement/typescript-angular2 |
…erfaces, but instead as classes. swagger-api#1334
…double. Updated test: enumArrayMdoelTest swagger-api#1334
…ons with non 204 (http status code) but empty http responses. swagger-api#1334
What are your compilation errors you are getting due to interfaces? You have to remember typescript compiles to js which has no clue about interfaces which means interfaces are a compile time thing. In that case interfaces allow you to create objects with type safety without needing a new operator. See above for example. Please provide the issue of why you get errors due to interfaces |
Replied in the PR. Thanks though for replying :) |
The templates in https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/resources/TypeScript-Angular should be updated to use TS 1.6 best practices:
I'd make a pull request if desired.
The text was updated successfully, but these errors were encountered: