fix(typescript): scope union aliases to same-namespace with >1 subclass#227
Open
muhabdulkadir wants to merge 6 commits into
Open
fix(typescript): scope union aliases to same-namespace with >1 subclass#227muhabdulkadir wants to merge 6 commits into
muhabdulkadir wants to merge 6 commits into
Conversation
…ve scalar names in fields and maps Signed-off-by: muhammed-abdulkadir <muhammed.abdulkadir@docusign.com>
Signed-off-by: muhammed-abdulkadir <muhammed.abdulkadir@docusign.com>
Contributor
Author
|
this pr is a follow-up for #226 |
Signed-off-by: Muhammed Abdulkadir <40035796+muhabdulkadir@users.noreply.github.com>
Signed-off-by: muhammed-abdulkadir <muhammed.abdulkadir@docusign.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the TypeScript code generator to stop emitting “singleton” union aliases and to scope generated union aliases/import behavior to subclasses within the same namespace, avoiding cross-namespace dependency inversion and import noise.
Changes:
- Union alias emission now requires >1 non-enum direct subclass in the same namespace.
flattenSubclassesToUnionnow applies the same “>1 same-namespace subclass” threshold before usingXUniontypes.- Removes cross-namespace subclass imports that previously existed solely to build cross-namespace unions.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/codegen/fromcto/typescript/typescriptvisitor.js | Updates/extends unit tests to reflect same-namespace-only union rules and removal of cross-namespace subclass imports. |
| test/codegen/snapshots/codegen.js.snap | Updates generated output snapshots to remove singleton unions and cross-namespace subclass import blocks. |
| lib/codegen/fromcto/typescript/typescriptvisitor.js | Implements same-namespace + “>1 subclass” threshold for union alias generation and for flattenSubclassesToUnion; removes cross-namespace subclass import generation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+294
to
+297
| if (sameNsSubclasses.length > 1) { | ||
| const useUnion = !(isEnumRef || isMapRef); | ||
| tsType = this.toTsType(field.getType(), !useUnion, useUnion); | ||
| } |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Muhammed Abdulkadir <40035796+muhabdulkadir@users.noreply.github.com>
Signed-off-by: muhammed-abdulkadir <muhammed.abdulkadir@docusign.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #
Union aliases were generated for single-subclass cases (producing useless singleton unions like
type XUnion = IChild) and pulled cross-namespace subclasses into the parent namespace, creating inverted dependencies, potential name collisions, and unbounded unions on system types like Concept.PS: Cross-namespace unions are a consumer-side concern; they compose the union from the specific subtypes they need.
Changes
export type XUnion = ...flattenSubclassesToUnionfield behavior applies the same >1 same-namespace thresholdBefore
Example 1: Duplicate members in union
concerto@1.0.0generates aConceptUnionthat includes cross-namespace subclasses. The result hasICategorymultiple times (one from each namespace that extends Concept):This union is unusable: TypeScript deduplicates the duplicate
ICategory, but the intent is ambiguous (whichICategory?), and none of these types are defined in this file's namespace.Example 2: Singleton unions are noise
Every parent with exactly one subclass gets a union that's just an alias for one type:
A union of one type adds no value;
AssetUnionis justIEquipment. Consumers can useIEquipmentdirectly.Example 3: Cross-namespace imports only existed to feed these unions
To build the broken unions, we imported every subclass from every namespace:
Separate import type blocks from the same namespace (not even consolidated), each with a circular-dependency warning. These imports were only needed for the singleton unions above; removing the broken unions removes the need for these imports entirely.
After
Example 1: No broken union
Example 2: No singleton unions
Example 3: No cross-namespace import noise
Related Issues
Author Checklist
--signoffoption of git commit.mainfromfork:branchname