-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow enum flattening #202
Allow enum flattening #202
Conversation
I'm converting this to a draft because I found a massive bug while testing further. My tests only had the enum itself in the struct, but adding other fields didn't work |
…hing to do with this feature and is recommended agains by MS even though TS-eslint advocates for it
This feature requires changing all uses of |
I have changed all the tests to account for this and added a few tests for the flattening of enums. Although all the tests are passing, I'm still not 100% sure I haven't broken anything |
I think this is fine. As far as I know, |
I have only ever seen declaration merging when I used the |
Alright! So let's drop this for now, I think that's fine. |
Thanks! |
I also think we need to figure out some better way to identify flattened enum fields, I did a whole lot of string manipulation there that I'm not sure will be able to handle everyone's use cases |
:D I meant declaration merging! Would love to support flattened enums, I think this effort is great! |
Great, I might make a separate PR to change |
Awesome, take your time!
I 100% agree! Once you think this is ready, feel free to convert it back to a PR again, then I'll take an other look. |
I think I figured it out. I created a new local branch to start from scratch and managed to get something working by converting all uses of This makes for slightly more redundant TS code but it seems to work. Example with the type Original = { b: { a: number, b: number, c: number, }, d: number, }
type New = { b: { c: number, } & { a: number, b: number, }, d: number, }
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ More verbose way to express the same type This pretty much eliminated the string manipulation mess I was previously doing |
@NyxCode should I close this PR and submit the new branch as a new one or transfer the code over here? |
Btw the extra intersection can be solved by adding type Original = { b: { a: number, b: number, c: number, }, d: number, }
type ExplicitIntersection = { b: { c: number, } & { a: number, b: number, }, d: number, }
type ImplicitIntersection = { b: { c: number, a: number, b: number, }, d: number, }
// ^^^^^^^^^ Less verbose, but the fields will still be out of order
// (non-flattened fields always come before flattened ones) This is possible because an enum will never be directly adjacent to a struct, as it will always be in parenthesis |
Awesome! Yeah, I don't see any issues with this whatsoever. The generated TS might be a bit more unintuitive that way, but I don't think anyone cares about that. |
Whatever you think is best! Generally, I think splitting a change like this into small PRs (doing required refactorings first, then implementing the feature) is a great idea. |
Great, in that case I'll close this PR and open a new one with the new branch |
I managed to change that in the latest commit |
This PR is aimed at allowing the use of
#[flatten]
in enum variants. It's not a very large change, but it does cause some code duplication, by redoing some of whatformat_variant
does insideenum_def
, so I'd like some ideas on how to mitigate that (maybe by changingformat_variant
to returnvariant_type
?)Fixes #96