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

Attribute for externally tagged enums or match serde by default #38

Closed
Heliozoa opened this issue Oct 4, 2021 · 4 comments
Closed

Attribute for externally tagged enums or match serde by default #38

Heliozoa opened this issue Oct 4, 2021 · 4 comments

Comments

@Heliozoa
Copy link
Contributor

Heliozoa commented Oct 4, 2021

Hi,
currently the the TS::decl() generated for a type like

#[derive(serde::Serialize, TS)]
enum Enum {
    A(&'static str),
    B { s: &'static str },
    C
}

looks like this

type Enum = string | {
    s: string,
} | "C";

whereas serde_json generates output matching the type

type Enum = { "A": string } | { "B": { s: string } } | "C"

serde-compat allows for matching other styles of representing enums, like #[serde(tag = "tag", content = "content")], but there's no way to match the default since there's no serde attribute for it. I think matching serde's default output is preferable since it allows for differentiating between A("C") and C, but an attribute would work too.

@NyxCode
Copy link
Collaborator

NyxCode commented Nov 8, 2021

I'm currently working on this.
In case you want to help, a few tests testing this behaviour, including its interaction with #[ts(inline)], would be tremendously usefull!

@NyxCode
Copy link
Collaborator

NyxCode commented Nov 8, 2021

What's working so far:

#[derive(TS)]
enum SimpleEnum {
    A(String),
    B(i32),
    C,
    D(String, i32),
    E(Foo),
    F { a: i32, b: String },
}

generates (output formatted)

type SimpleEnum =
  | { A: string }
  | { B: number }
  | "C"
  | { D: [string, number] }
  | { E: Foo }
  | { F: { a: number, b: string } };

Which does seem to match serdes default "externally tagged" representation:

{"A":"foo"}
{"B":5}
"C"
{"D":["bar",123]}
{"E":{"bar":{"field":44}}}
{"F":{"a":42,"b":"baz"}}

@NyxCode
Copy link
Collaborator

NyxCode commented Nov 8, 2021

though #[ts(inline)] is not behaving like it should

@NyxCode
Copy link
Collaborator

NyxCode commented Nov 11, 2021

fixed in v6.0.0

@NyxCode NyxCode closed this as completed Nov 11, 2021
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