fix(typescript): generate scalar decalration types and tighten union generation to require multiple local subclasses#209
Conversation
There was a problem hiding this comment.
Pull request overview
Updates the TypeScript from-CTO code generator to improve type fidelity for Concerto scalars and to reduce noise from singleton union aliases, keeping unions and imports scoped to same-namespace subclasses.
Changes:
- Emit scalar declarations as exported TypeScript type aliases and preserve scalar names when emitting scalar-typed fields.
- Tighten class union alias generation and
flattenSubclassesToUnionbehavior to require multiple same-namespace subclasses (excluding cross-namespace subclasses). - Update TypeScript generator tests and Jest snapshots to reflect the new output (fewer subclass imports/warnings, fewer singleton unions, new scalar aliases).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/codegen/fromcto/typescript/typescriptvisitor.js | Adds scalar declaration emission; preserves scalar alias names for fields; restricts union generation to multiple same-namespace subclasses; removes cross-namespace subclass import behavior. |
| test/codegen/fromcto/typescript/typescriptvisitor.js | Updates/extends unit tests for tightened union logic and new scalar generation/field behavior. |
| test/codegen/fromcto/data/model/comprehensive.cto | Adds a comprehensive CTO fixture covering many declaration types (scalars/enums/maps/resources/etc.). |
| test/codegen/snapshots/codegen.js.snap | Updates snapshots for removed singleton unions/warnings and new scalar aliases/import changes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (!properties.has(typeNamespace)) { | ||
| properties.set(typeNamespace, new Set()); | ||
| } | ||
| properties.get(typeNamespace).add(property.isTypeEnum?.() ? typeName : `I${typeName}`); | ||
| // Scalars and enums use their plain name; classes use the I prefix | ||
| properties.get(typeNamespace).add((property.isTypeEnum?.() || property.isTypeScalar?.()) ? typeName : `I${typeName}`); | ||
| } |
There was a problem hiding this comment.
Map type references are still treated like class/interface references when collecting imports: for non-primitive properties you add I${typeName} unless it’s an enum or scalar. However visitMapDeclaration emits maps as export type ${MapName} = Map<...>, so importing I${MapName} will reference a non-existent export and can break the generated TS (e.g., snapshots show employeeTShirtSizes?: EmployeeTShirtSizes; but imports include IEmployeeTShirtSizes). Consider detecting map declarations (via modelManager.getType(property.getFullyQualifiedTypeName()).isMapDeclaration?.()) and importing the plain typeName like scalars/enums.
|
Thanks for this contribution! Before we can review, please resolve the merge conflicts with the base branch. git fetch origin main
git rebase origin/main
# Resolve conflicts
git push --force-with-leaseOnce the conflicts are resolved, we'll proceed with the review. This comment was generated by AI on behalf of @mttrbrts. |
Closes #
This PR updates the TypeScript from-CTO visitor to improve generated type fidelity in two areas:
Problem
The previous behavior had two gaps:
Changes
Screenshots or Video
Related Issues
Author Checklist
--signoffoption of git commit.mainfromfork:branchname