From d1223739632db2382eb419dc67e8cdac95d6430b Mon Sep 17 00:00:00 2001 From: escritorio-gustavo <131818645+escritorio-gustavo@users.noreply.github.com> Date: Thu, 18 Apr 2024 09:25:01 -0300 Subject: [PATCH] Handle case where `TypePath::qself` is `Some` (#310) --- macros/src/attr/field.rs | 8 ++++++-- ts-rs/tests/integration/infer_as.rs | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/macros/src/attr/field.rs b/macros/src/attr/field.rs index 1417777..dd8552c 100644 --- a/macros/src/attr/field.rs +++ b/macros/src/attr/field.rs @@ -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 => (), diff --git a/ts-rs/tests/integration/infer_as.rs b/ts-rs/tests/integration/infer_as.rs index 459aaac..09fa4c5 100644 --- a/ts-rs/tests/integration/infer_as.rs +++ b/ts-rs/tests/integration/infer_as.rs @@ -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, }" + ); }