Handle case where TypePath::qself is Some (#310)

This commit is contained in:
escritorio-gustavo
2024-04-18 09:25:01 -03:00
committed by GitHub
parent cb1314d87c
commit d122373963
2 changed files with 21 additions and 3 deletions
+6 -2
View File
@@ -1,5 +1,5 @@
use syn::{
AngleBracketedGenericArguments, Attribute, Field, GenericArgument, Ident, PathArguments,
AngleBracketedGenericArguments, Attribute, Field, GenericArgument, Ident, PathArguments, QSelf,
Result, ReturnType, Type, TypeArray, TypeGroup, TypeParen, TypePath, TypePtr, TypeReference,
TypeSlice, TypeTuple,
};
@@ -232,7 +232,11 @@ fn replace_underscore(ty: &mut Type, with: &Type) {
replace_underscore(elem, with);
}
}
Type::Path(TypePath { path, qself: None }) => {
Type::Path(TypePath { path, qself }) => {
if let Some(QSelf { ty, .. }) = qself {
replace_underscore(ty, with);
}
for segment in &mut path.segments {
match &mut segment.arguments {
PathArguments::None => (),
+15 -1
View File
@@ -2,14 +2,28 @@
use ts_rs::TS;
trait Bar {
type Baz;
}
impl Bar for String {
type Baz = i32;
}
#[derive(TS)]
#[ts(export)]
struct Foo {
#[ts(optional, as = "Option<_>")]
my_optional_bool: bool,
#[ts(as = "<_ as Bar>::Baz")]
q_self: String,
}
#[test]
fn test() {
assert_eq!(Foo::inline(), "{ my_optional_bool?: boolean, }");
assert_eq!(
Foo::inline(),
"{ my_optional_bool?: boolean, q_self: number, }"
);
}