Repository: Aleph-Alpha/ts-rs
Pull request #281 (2024-03-22 09:24:14)

Fix generic impl
## Goal

When deriving `ts_rs::TS` for a struct with a generic type parameter, the code expanded to requiring the `ts_rs::TS` trait to be in scope.  
This should be fixed.

## Changes

This PR fixes this by using the newly introduced `crate_rename` path, interpolating the path to be `#crate_rename::TS`

## Checklist

- [x] I have followed the steps listed in the [Contributing guide](https://github.com/Aleph-Alpha/ts-rs/blob/main/CONTRIBUTING.md).
- [x] If necessary, I have added documentation related to the changes made.
- [x] I have added or updated the tests related to the changes made.



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e0ee3f0..12adc59 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,8 @@
 
 ### Fixes
 
+- Macro expansion for types with generic parameters now works without the `TS` trait in scope ([#281](https://github.com/Aleph-Alpha/ts-rs/pull/281))
+
 # v8.0.0
 
 ### Breaking
diff --git a/macros/src/attr/struct.rs b/macros/src/attr/struct.rs
index ce58a0e..bd177f6 100644
--- a/macros/src/attr/struct.rs
+++ b/macros/src/attr/struct.rs
@@ -3,9 +3,8 @@ use std::collections::HashMap;
 use syn::{parse_quote, Attribute, Ident, Path, Result, Type, WherePredicate};
 
 use super::{parse_assign_from_str, parse_bound, parse_concrete};
-use crate::attr::EnumAttr;
 use crate::{
-    attr::{parse_assign_str, Inflection, VariantAttr},
+    attr::{parse_assign_str, EnumAttr, Inflection, VariantAttr},
     utils::{parse_attrs, parse_docs},
 };
 
diff --git a/macros/src/lib.rs b/macros/src/lib.rs
index 5370e46..b4895d5 100644
--- a/macros/src/lib.rs
+++ b/macros/src/lib.rs
@@ -140,6 +140,7 @@ impl DerivedTS {
     /// impl ts_rs::TS for B { /* .. */ }
     /// ```
     fn generate_generic_types(&self, generics: &Generics) -> TokenStream {
+        let crate_rename = &self.crate_rename;
         let generics = generics
             .type_params()
             .filter(|ty| !self.concrete.contains_key(&ty.ident))
@@ -154,7 +155,7 @@ impl DerivedTS {
                         write!(f, "{:?}", self)
                     }
                 }
-                impl TS for #generics {
+                impl #crate_rename::TS for #generics {
                     type WithoutGenerics = #generics;
                     fn name() -> String { stringify!(#generics).to_owned() }
                     fn inline() -> String { panic!("{} cannot be inlined", Self::name()) }
diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs
index 5294013..8b8cda0 100644
--- a/ts-rs/src/lib.rs
+++ b/ts-rs/src/lib.rs
@@ -138,11 +138,11 @@ pub use ts_rs_macros::TS;
 pub use crate::export::ExportError;
 use crate::typelist::TypeList;
 
-#[cfg(feature = "serde-json-impl")]
-mod serde_json;
 #[cfg(feature = "chrono-impl")]
 mod chrono;
 mod export;
+#[cfg(feature = "serde-json-impl")]
+mod serde_json;
 pub mod typelist;
 
 /// A type which can be represented in TypeScript.  


Test patch:
diff --git a/ts-rs/tests/generic_without_import.rs b/ts-rs/tests/generic_without_import.rs
new file mode 100644
index 0000000..d147048
--- /dev/null
+++ b/ts-rs/tests/generic_without_import.rs
@@ -0,0 +1,6 @@
+#![allow(dead_code)]
+
+#[derive(ts_rs::TS)]
+struct Test<T> {
+    field: T,
+}
diff --git a/ts-rs/tests/serde_json.rs b/ts-rs/tests/serde_json.rs
index 08598fe..29e32c7 100644
--- a/ts-rs/tests/serde_json.rs
+++ b/ts-rs/tests/serde_json.rs
@@ -61,5 +61,5 @@ fn inlined_value() {
 #[derive(TS)]
 #[ts(export, export_to = "serde_json_impl/")]
 struct Simple {
-    json: serde_json::Value
+    json: serde_json::Value,
 }