-
Notifications
You must be signed in to change notification settings - Fork 138
Fix trait bound generation when using #[ts(optional)] on an Option<Generic>
#454
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
base: main
Are you sure you want to change the base?
Conversation
#[ts(optional)] on an Option<Generic>
| GenericOptionalStruct::<()>::decl(), | ||
| "type GenericOptionalStruct<T> = { a?: T, b?: T | null, c: T | null, d: T, e: number | null, f: number, };" | ||
| ) | ||
| } |
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.
The integration test will work with or without the changes.
Please use my example from the issue:
#[derive(Serialize, TS)]
#[ts(export, export_to = "optional_field/")]
struct GenericOptionalStruct<T1, T2>
{
#[ts(optional)] // <- error is happening here
a: Option<T2>,
#[ts(optional = nullable)]
b: Option<T1>,
c: Option<T1>,
}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.
Ah, you're right, I tried adding more fields after I finished the fix to be thorough but I forgot that the test only fails if the ONLY field that mentions T is the one with #[ts(optional)], i.e., lines 31 - 34 ruin this test. I'll remove the extra fields
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.
I'll keep the current type though because I don't want the test to imply that the issue comes from having 2 or more generics
|
I have also just found out that the reason the type I've changed |
Goal
Fix dependency tracking for generic type when using
#[ts(optional)]Using
#[ts(optional)]on a field of typeOption<T>, whereTis an actual generic type in the struct declaration, caused the generation of theimplblock to fail to include thewhere T: TSclause. This was caused by the fact that theused_type_paramsfunction couldn't see through the type generated by the application of#[ts(optional)], which is<T as ::ts_rs::TS>::OptionInnerTypeand would fail to detect the use of the type parameterTTo be clearer, the following type
Would generate the following
implblock:Instead of the correct
implblock:Closes #453
Changes
Added a match arm in
used_type_paramsthat handles the caseType::Path(TypePath { qself: Some(_), .. })I also found a way to simplify the compile-time checking that ensures
#[ts(optional)]cannot be used in non-option fields, with the added bonus that it moves the compiler error to the field's type instead of the attribute itself and makes it a lot shorter:Before

After

Checklist