Open ext opened 4 years ago
👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.
A notable difference is that using <any>
makes TS to treat this enum as a numeric enum, which means the compiled JS code includes both forward and reverse lookup (https://www.typescriptlang.org/play?jsx=0&ts=3.7.5#code/KYOwrgtgBAYg9nAouaBvAUFKBBKBeKAHgEMQBPAPigHIAhagGk1gHkX8jTKbbsAlaugC+6dKEiwEySACYoGLLgJ1GzGGw51+gkUA)
This doesn't matter if enum keys are the same as values, like in your example (FOO = "FOO"
). But in a real generated
code it's not always the case (e.g. if using enumPropertyNaming
generator option other than "original"
, or when a value is not a valid identifier).
I personally find this <any>
awkward - and have recently got rid of it in our project.
@amakhrov Hi how did you get rid of the <any>
in your project? is there an openapi-generator configuration?
We use a custom template, which we pass to the generator via --template-dir
command line argument.
Is there an open-source location to the custom template? Our team are running into conflicting type issues resulting from enums generated from this <any>
running through typescript-node
, it would be awesome to use either a configuration or if not then the custom template for generating enums that matches our expectation.
Built-in templates for typescript-node
are here: https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/typescript-node
And here is the line that adds those <any>
typer assertions: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-node/model.mustache#L62
When you provide custom templates via --template-dir
, this dir doesn't have to have ther full set of templates. You can only have a single one there that is tweaked for your needs (model.mustache
in this case), and for everything else use built-in files.
Description
Given an API with an enum:
the
typescript-angularjs
(and other typescript templates) generates enums such as:I'm not sure about that
<any>
casting as it produces a different API than without it.When compiling to
.js
/.d.ts
the output is vastly different because of the<any>
cast:This could be argued to be a bug in
tsc
but on the other hand I dont see the purpose of the casting and what the intended outcome is. By looking at just the type information bothFOO
andBAR
can be anything sotsc
is not entirely wrong either.It also seems to differ with other typescript templates:
openapi-generator version
Tested 4.2.2 and 4.3.0, same issue.
OpenAPI declaration file content or url
openapi.yaml
:tsconfig.json
:Command line used for generation
Steps to reproduce
dist/typescript-angularjs/model/Body.d.ts
Suggest a fix
I personally think the cast should be removed and I dont see what it intends to solve. I can gladly create a PR to fix this if someone can acknowledge the issue.