Skip to content
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

Incorrect output when using serde tags #214

Closed
d4hines opened this issue Jan 31, 2024 · 3 comments
Closed

Incorrect output when using serde tags #214

d4hines opened this issue Jan 31, 2024 · 3 comments

Comments

@d4hines
Copy link

d4hines commented Jan 31, 2024

Hello,
I noticed that when using #[serde(tag = ...)] like so:

#[derive(Type)]
#[serde(tag = "type")]
pub enum MyEnum {
    A(Option<MyStruct>),
    B,
}

the outputted type is incorrect.

I've created a small reproducer here that makes the following output. On the left is actual, right is what I expect:

assertion `left == right` failed
  left: "export type MyEnum = ({ type: \"A\" } & MyStruct | null) | { type: \"B\" }"
 right: "export type MyEnum = ({ type: \"A\" } & (MyStruct | null)) | { type: \"B\" }"

I'm not sure if this serde features is supported yet, but figured I'd report here anyway.

@d4hines
Copy link
Author

d4hines commented Jan 31, 2024

The issue seems to be that & binds tighter than | and the printer isn't accounting for that (incidentally, can't even find a Typescript type operator precedence table with google 🤷‍♂️). Maybe inserting parens on around | would be a sufficient fix?

@d4hines
Copy link
Author

d4hines commented Jan 31, 2024

This seems like a hack, but it works for our use case: d4hines@b280b6a

@oscartbeaumont
Copy link
Member

Great spot but from the looks of it this is a duplicate of #174 & 467e2b0 which have been fixed on Specta v2.

However, it's worth noting your type also doesn't parse the new Serde validation built into Specta v2 so you may have problems just upgrading. This extra checking will be optional for final release but right now it's required (#217).

You can checkout this Rust playground to see the runtime errors Serde would spit out: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=34f9c718cf77f18e8d47ade4e6d76ea5

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 a pull request may close this issue.

2 participants