-
Notifications
You must be signed in to change notification settings - Fork 117
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
compile-time checked #[ts(optional)]
#367
compile-time checked #[ts(optional)]
#367
Conversation
This is achieved (technically semver-breaking) by adding an associated type, With that, the behaviour of This is done by emitting this code: use std::marker::PhantomData;
let actual: PhantomData<#ty> = PhantomData;
let must: PhantomData<Option<_>> = actual; If |
Alternative way to provoke a compile-time error: fn a<T>(_: std::option::Option<T>) {}
fn b(x: #ty) { a(x) } |
Notably, what unfortunately does not work is something like this: let true = <#ty as TS>::IS_OPTION; |
I massively improved the error. For #[derive(TS)]
#[ts(export, export_to = "optional_field/", optional)]
struct OptionalStruct {
#[ts(optional = nullable)]
d: i32,
} we now get
There is a weird note attached to the error though, which I don't know how to get rid of:
I think that's what rust-lang/rust#51992 will be for. |
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.
Dude, this is awesome! You fixed all the issues we had with Option in the first place. I love that you got the compiler to check #[ts(optional)]
with a great error message
dcc7a46
into
add_ts_optional_to_struct
With how good the error message is overall, I don't think this is a problem |
Builds on top of #366.
#[ts(optional)]
is used on a non-Option
field. The error is not great yet - it's probably possible to improve this though.