Skip to content

[emitter-framework] Render discriminated unions correctly #7369

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

maorleger
Copy link
Member

@maorleger maorleger commented May 15, 2025

This pull request enhances the handling of discriminated unions in the @typespec/emitter-framework package. It introduces new rendering logic for discriminated unions, updates existing components to support these changes, and improves test coverage to validate the new functionality.

Enhancements to discriminated union rendering:

  • Added new rendering logic for discriminated unions, including support for "object" and "none" envelope styles, through the ObjectEnvelope and NoneEnvelope functions in union-expression.tsx. These handle different discriminator configurations and ensure proper rendering of union variants. [1] [2]

Updates to existing components:

  • Modified the InterfaceBody component in interface-declaration.tsx to include a semicolon when rendering type members, ensuring consistency with TypeScript syntax. Found in passing and validated via the new tests

Improvements to test coverage:

  • Refactored and expanded tests in union-declaration.test.tsx using the new testing patterns

Fixes #7174

Copy link
Contributor

github-actions bot commented May 15, 2025

All changed packages have been documented.

  • @typespec/emitter-framework
Show changes

@typespec/emitter-framework - fix ✏️

Render discriminated unions correctly

@azure-sdk
Copy link
Collaborator

azure-sdk commented May 15, 2025

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@maorleger maorleger marked this pull request as ready for review May 15, 2025 22:14

const [namespace] = program.resolveTypeReference("DemoService");
const union = Array.from((namespace as Namespace).enums.values())[0];
it("renders a discriminated union declaration with no envelope", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does it looks like in thoses cases when you use a named model?

},
});

return <TypeExpression type={model} />;
Copy link
Member

@timotheeguerin timotheeguerin May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better type might be to create a typescript intersection here for each variant.

type Pet = {kind: "a"} & Cat | {kind: "b"} & Dog

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or at least if the model has a name

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: TypeScript Emitter Framework does not handle new discriminated unions
3 participants