Skip to content

Allow flattening newtypes and as types#477

Open
cairomassimo wants to merge 1 commit intoAleph-Alpha:mainfrom
cairomassimo:fix-flatten-newtype-and-type-as
Open

Allow flattening newtypes and as types#477
cairomassimo wants to merge 1 commit intoAleph-Alpha:mainfrom
cairomassimo:fix-flatten-newtype-and-type-as

Conversation

@cairomassimo
Copy link

Goal

  1. Allow flattening newtypes if the inner type can be flattened. This matches serde_json behavior.
  2. Allow flattening types with #[ts(as = "...")] if the referenced type can be flattened. This can be useful and (as far as I can tell) has no downsides.

Changes

  • macros/src/types/newtype.rs: Add inline_flattened that delegates to the inner type
  • macros/src/types/type_as.rs: Add inline_flattened that delegates to the as type
  • macros/src/types/enum.rs: Restructure internally tagged enum handling to check for newtype variants first, ensuring they always use intersection syntax ({ "tag": "A" } & Type) rather than the flattened path
  • Added tests in newtype_flatten.rs and type_as_flatten.rs

Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@gustavo-shigueo
Copy link
Collaborator

Hi @cairomassimo thanks for the PR! We are already making a change (#474) to how we flatten structs with zero fields that touches some of the same areas of the code as you have here - specifically, the match expression in the enum file. Would you mind waiting until those changes are merged as to help us avoid a possible conflict and breaking change?

@cairomassimo
Copy link
Author

cairomassimo commented Feb 8, 2026 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants