-
Notifications
You must be signed in to change notification settings - Fork 115
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
Add #[ts(optional)]
to struct
#366
base: main
Are you sure you want to change the base?
Conversation
Love the idea. Would also like to hear what @Nutomic and @dessalines think. I'm not yet sure if re-using the same |
Excellent! Yep this would really gonna clean up our structs. Thx! |
@gustavo-shigueo A problem I see with this implementation is that all fields have to be We could fix that by trying to figure out if a field is an Maybe we can move that logic from the macro to the runtime, where we actually know if a type is an Option or not. |
Damn, I forgot about that
Yeah, that would probably not be a great idea |
I implemented a band-aid fix for the problem, but we should probably look into a better way to detect the |
We could add a |
@NyxCode I have implemented this idea for you to take a look, I also change |
The downside of this implementation is that there is no compile time error for #[derive(TS)]
struct Foo {
#[ts(optional)]
foo: i32
} Instead, this will panic when the user attempts to export their types. I don't quite like this because ts-rs usually works in a "If it compiles, you're good" way, but here we get something that compiles and doesn't work. |
Nice work! I have an idea how to get this compile-time checked, lemme see if that actually works though.. |
macros/src/types/named.rs
Outdated
let optional_annotation = if let Optional::Optional { .. } = field_attr.optional { | ||
quote! { | ||
if <#ty as #crate_rename::TS>::IS_OPTION { | ||
#optional_annotation | ||
} else { | ||
panic!("`#[ts(optional)]` can only be used with the Option type") | ||
} | ||
} | ||
Optional { | ||
optional: false, .. | ||
} => (&parsed_ty, ""), | ||
} else { | ||
optional_annotation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option is just remove this whole thing, this way there'll be no panic (still, no compiler error). What will happen is that #[ts(optional)]
will just do nothing when not using an Option type
Good point, maybe at the struct level we should call it something like |
@NyxCode I've applied this change, but if you think of a better name, feel free to push a commit here |
I also added the breaking change tag because this will break manual implementations of TS. As always, the feature we need to avoid that is unstable lol (rust-lang/rust#29661) |
Goal
Allow the use of
#[ts(optional)]
on struct definitionsCloses #364
Checklist