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

Impossible type generated for tagged enum variant with anonymous field #24

Closed
link2xt opened this issue Aug 3, 2023 · 3 comments
Closed

Comments

@link2xt
Copy link

link2xt commented Aug 3, 2023

I have tried adding this test:

diff --git a/tests/test.rs b/tests/test.rs
index cc14ae8..5480c3a 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -246,6 +246,7 @@ export type Test10=({"type":"A";"value":(types.Parent&{"a":string;"b":types.Usiz
                 B(Inner),
                 // C(Inner, Inner), // not allowed
                 D,
+                E(i64),
             }
 
             assert_eq_str!(

This results in generation of the following type:

export type I64=number;
export type Test=(({"type":"A";}&{"a":types.Inner;})|({"type":"B";}&types.Inner)|{"type":"D";}|({"type":"E";}&types.I64));

Type ({"type":"E";}&types.I64) does not look like something possible to construct.

This is a minimal example of what I am trying to do at deltachat/deltachat-core-rust#4602

No idea how this type should look like, maybe should not be allowed similar to C.

@dbeckwith
Copy link
Owner

A type like this is not even supported by serde_json. When I try to serialize a value of this type, I get an error cannot serialize tagged newtype variant Test::E containing an integer. Since the goal of this library is to produce TypeScript types that describe the way serde_json serializes values, ({"type":"E";}&types.I64) is technically correct because its effectively never, and serde_json cannot serialize values of this type.

Maybe I could update my library to catch this case and throw an error or emit never just as a quality-of-life improvement though.

@dbeckwith
Copy link
Owner

dbeckwith commented Aug 5, 2023

That same test has a commented-out variant // C(Inner, Inner), // not allowed demonstrating another valid Rust type that serde_json does not support (and neither does this library, although it may emit nonsensical types for it).

@dbeckwith
Copy link
Owner

Thinking about how I might catch this case in my library, I think it's not possible to do statically since it's hard to tell in general if the type i64 in E(i64) is a struct or a primitive. I think this is the same reason serde doesn't emit an error in its derive macro for this case, but it does for C(Inner, Inner).

@dbeckwith dbeckwith closed this as not planned Won't fix, can't repro, duplicate, stale Aug 5, 2023
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

No branches or pull requests

2 participants