Skip to content

Commit

Permalink
Fix generic impl (#281)
Browse files Browse the repository at this point in the history
* Fix rename impl for generics
* Add regression test
  • Loading branch information
aumetra authored Mar 22, 2024
1 parent 2d5d511 commit 88c2ae8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions macros/src/attr/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down
3 changes: 2 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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()) }
Expand Down
4 changes: 2 additions & 2 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions ts-rs/tests/generic_without_import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(dead_code)]

#[derive(ts_rs::TS)]
struct Test<T> {
field: T,
}
2 changes: 1 addition & 1 deletion ts-rs/tests/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit 88c2ae8

Please sign in to comment.