Aleph-Alpha / ts-rs

Generate TypeScript bindings from Rust types
MIT License
989 stars 99 forks source link

Support `#[ts(as = "...")]` and `#[ts(type = "...")]` on enum variants #284

Open escritorio-gustavo opened 3 months ago

escritorio-gustavo commented 3 months ago

Goal

Add support for using #[ts(as = "...")] and #[ts(type = "...")] on enum variants to make support for #[serde(with = "...")] (#280) possible here too

Changes

#[ts(as = "...")] and #[ts(type = "...")] are now parsed and processed in enum variants.

Using them currently respects the tag enum representation. That is, neither #[ts(as = "...")] nor #[ts(type = "...")] will eliminate the "tag" or "content" properties the variant would have without them, as that can be achieved by using #[ts(untagged)] on the variant. I don't yet know if this is how serde works, so this can be changed to maintain serde compatibility

Checklist

escritorio-gustavo commented 3 months ago

I may need some help on the tests for this. I can't even get #[serde(with = "...")] on variants to compile atm

NyxCode commented 3 months ago

would love to take a loot at this once i find some time 👍

escritorio-gustavo commented 2 months ago

@NyxCode, I think I figured out how #{serde(with = ...)] is used in variants. Looking at the last commit, do you think the current behavior of #[ts(as = ...)] is correct? I think it is, but I still don't quite understand #{serde(with = ...)] deeply enough to be sure