diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 35a07cc..6c874c7 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -144,7 +144,7 @@ impl DerivedTS { } } impl TS for #generics { - type Generics = #generics; + type WithoutGenerics = #generics; fn name() -> String { stringify!(#generics).to_owned() } fn transparent() -> bool { false } } @@ -263,9 +263,9 @@ fn generate_assoc_type(rust_ty: &Ident, generics: &Generics) -> TokenStream { .collect::>(); if generics_params.is_empty() { - quote! { type Generics = #rust_ty; } + quote! { type WithoutGenerics = #rust_ty; } } else { - quote! { type Generics = #rust_ty<#(#generics_params),*>; } + quote! { type WithoutGenerics = #rust_ty<#(#generics_params),*>; } } } diff --git a/ts-rs/src/chrono.rs b/ts-rs/src/chrono.rs index 9ab0c3f..e5acfd7 100644 --- a/ts-rs/src/chrono.rs +++ b/ts-rs/src/chrono.rs @@ -11,7 +11,7 @@ use super::{impl_primitives, TS}; macro_rules! impl_dummy { ($($t:ty),*) => {$( impl TS for $t { - type Generics = $t; + type WithoutGenerics = $t; fn name() -> String { String::new() } fn inline() -> String { String::new() } fn transparent() -> bool { false } @@ -23,7 +23,7 @@ impl_primitives!(NaiveDateTime, NaiveDate, NaiveTime, Month, Weekday, Duration = impl_dummy!(Utc, Local, FixedOffset); impl TS for DateTime { - type Generics = Self; + type WithoutGenerics = Self; fn ident() -> String { "string".to_owned() } @@ -39,7 +39,7 @@ impl TS for DateTime { } impl TS for Date { - type Generics = Self; + type WithoutGenerics = Self; fn ident() -> String { "string".to_owned() } diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index 1ad2b68..0071707 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -152,7 +152,7 @@ pub mod __private { pub(crate) fn export_type_to_string() -> Result { let mut buffer = String::with_capacity(1024); buffer.push_str(NOTE); - generate_imports::(&mut buffer)?; + generate_imports::(&mut buffer)?; generate_decl::(&mut buffer); Ok(buffer) } diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 702beef..99a185f 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -275,12 +275,26 @@ pub mod typelist; /// - `#[ts(skip)]`: /// Skip this variant pub trait TS { - type Generics: TS + ?Sized; + /// If this type does not have generic parameters, then `WithoutGenerics` should just be `Self`. + /// If the type does have generic parameters, then all generic parameters must be replaced with + /// a dummy type, e.g `ts_rs::Dummy` or `()`. + /// The only requirement for these dummy types is that `EXPORT_TO` must be `None`. + /// Example: + /// ```ignore + /// struct GenericType(A, B); + /// impl TS for GenericType { + /// type WithoutGenerics = GenericType; + /// // ... + /// } + /// ``` + type WithoutGenerics: TS + ?Sized; const EXPORT_TO: Option<&'static str> = None; const DOCS: Option<&'static str> = None; + /// Identifier of this type, excluding generic parameters. fn ident() -> String { + // by default, fall back to `TS::name()`. let name = Self::name(); if name.contains('<') { @@ -312,7 +326,7 @@ pub trait TS { panic!("{} cannot be declared", Self::name()); } - /// Name of this type in TypeScript. + /// Name of this type in TypeScript, including generic parameters fn name() -> String; /// Formats this types definition in TypeScript, e.g `{ user_id: number }`. @@ -436,7 +450,7 @@ impl Dependency { macro_rules! impl_primitives { ($($($ty:ty),* => $l:literal),*) => { $($( impl TS for $ty { - type Generics = Self; + type WithoutGenerics = Self; fn name() -> String { $l.to_owned() } fn inline() -> String { ::name() } fn transparent() -> bool { false } @@ -447,7 +461,7 @@ macro_rules! impl_primitives { macro_rules! impl_tuples { ( impl $($i:ident),* ) => { impl<$($i: TS),*> TS for ($($i,)*) { - type Generics = (Dummy, ); + type WithoutGenerics = (Dummy, ); fn name() -> String { format!("[{}]", [$($i::name()),*].join(", ")) } @@ -474,7 +488,7 @@ macro_rules! impl_tuples { macro_rules! impl_wrapper { ($($t:tt)*) => { $($t)* { - type Generics = Self; + type WithoutGenerics = Self; fn name() -> String { T::name() } fn inline() -> String { T::inline() } fn inline_flattened() -> String { T::inline_flattened() } @@ -499,7 +513,7 @@ macro_rules! impl_wrapper { macro_rules! impl_shadow { (as $s:ty: $($impl:tt)*) => { $($impl)* { - type Generics = <$s as TS>::Generics; + type WithoutGenerics = <$s as TS>::WithoutGenerics; fn name() -> String { <$s>::name() } fn inline() -> String { <$s>::inline() } fn inline_flattened() -> String { <$s>::inline_flattened() } @@ -515,7 +529,7 @@ macro_rules! impl_shadow { } impl TS for Option { - type Generics = Self; + type WithoutGenerics = Self; fn name() -> String { format!("{} | null", T::name()) } @@ -540,7 +554,7 @@ impl TS for Option { } impl TS for Result { - type Generics = Result; + type WithoutGenerics = Result; fn name() -> String { format!("{{ Ok : {} }} | {{ Err : {} }}", T::name(), E::name()) } @@ -569,7 +583,7 @@ impl TS for Result { } impl TS for Vec { - type Generics = Vec; + type WithoutGenerics = Vec; fn ident() -> String { "Array".to_owned() } @@ -599,7 +613,7 @@ impl TS for Vec { // Arrays longer than this limit will be emitted as Array const ARRAY_TUPLE_LIMIT: usize = 64; impl TS for [T; N] { - type Generics = [Dummy; N]; + type WithoutGenerics = [Dummy; N]; fn name() -> String { if N > ARRAY_TUPLE_LIMIT { return Vec::::name(); @@ -642,7 +656,7 @@ impl TS for [T; N] { } impl TS for HashMap { - type Generics = HashMap; + type WithoutGenerics = HashMap; fn ident() -> String { "Record".to_owned() } @@ -676,7 +690,7 @@ impl TS for HashMap { } impl TS for Range { - type Generics = Range; + type WithoutGenerics = Range; fn name() -> String { format!("{{ start: {}, end: {}, }}", I::name(), I::name()) } @@ -694,7 +708,7 @@ impl TS for Range { } impl TS for RangeInclusive { - type Generics = RangeInclusive; + type WithoutGenerics = RangeInclusive; fn name() -> String { format!("{{ start: {}, end: {}, }}", I::name(), I::name()) } @@ -793,7 +807,7 @@ impl std::fmt::Display for Dummy { } impl TS for Dummy { - type Generics = Self; + type WithoutGenerics = Self; fn name() -> String { "Dummy".to_owned() }