avoid namespace imports for enums #8755
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR slightly modifies TypeScript code-generation to use idiomatic ES6-style imports (where possible) when importing custom
enumValues
.Currently, the following config:
will produce
The namespace import (on the second line above) is a fairly unusual bit of TypeScript syntax. Some third-party parsers (such as Babel) have difficulty parsing it. In this scenario, the namespace import is also entirely unnecessary, as we can specify our alias in the original import statement.
After this PR, the output becomes:
This output should be equivalent in terms of functionality, is slightly more readable and compact, and should be better-tolerated by tools that do not understand TypeScript namespaces.
MyCustomEnum
is no longer available as a variable in the generated file, although I would not imagine that any reasonable person would expect it to be. (If anything, this change slightly reduces the odds of unintentional naming collisions)This PR does not attempt to resolve this issue for deeply-nested imports such as:
I'm less confident in my ability to write 100%-equivalent code here that does not utilize namespace imports, and I would also imagine that this scenario is considerably less common.
Related #4236
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
This functionality is covered by existing unit tests, which have been modified to reflect the new expected output.
Changes to the generated code should be no more complicated than the example described above.
Checklist: