-
Notifications
You must be signed in to change notification settings - Fork 129
Add #[ts(optional)]
to struct
#366
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
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 |
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.
Hey @gustavo-shigueo! I am - and have been - very busy lately. Sorry for being less active here.
Thank you for keeping an eye on everything!
Nonetheless, I'd like to get this merged, and release a new version in a new PR.
The only thing missing here is a note in the changelog, I think. Is there something else you'd like to change/note/add?
Hey @NyxCode, I hope you're doing well! No problem, I've also been pretty busy lately, I've been keeping an eye on the issues but haven't written any code in a while.
Awesome! I'll make sure to add it tomorrow morning, thanks! |
Updated the changelog, I also ran |
I'll see if I can make a GH workflow this weekend to check that for us in the future |
Would it be possible to do a minor release sometime soon (that has this)? |
This is a breaking change for anyone manually implementing |
I think it could be possible to have a major release about now, the only PR that I think is worth looking into before hand is #376, which is waiting for @NyxCode's review, and maaaaybe we could look into having a pre-release version of the CLI, but that's unlikely, due to the concerns raised in #382 and the fact that I'm currently unable to push commits to the CLI branch |
@gustavo-shigueo I've raised the permissions issue with the org. They reshuffled the management of it, but I hope they'll finally get around to it after the easter holidays. Again, sorry about that. I'll merge the deno PR & cut a release early next week. |
Thanks for looking into it! No need to apologize, though. This isn't on you at all
Awesome! |
Hey @NyxCode, just checking if have you've heard anything from them |
Hey @gustavo-shigueo! |
Hey @NyxCode, yeah, I just received it, thanks! |
Yay! Man, that was a journey. Sorry that it took so long. |
Don't worry about it |
Happy to say we'll be using this feature soon in lemmy: LemmyNet/lemmy#5777 Thx again! |
Goal
Allow the use of
#[ts(optional)]
on struct definitionsCloses #364
Checklist