Skip to content

Commit

Permalink
Merge pull request #216 from escritorio-gustavo/inline_generics_default
Browse files Browse the repository at this point in the history
Inline generics with default types works, as long as there are no trait bounds
  • Loading branch information
escritorio-gustavo committed Jan 30, 2024
2 parents 5a5b518 + 94bb267 commit a0a3e13
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion macros/src/types/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn format_type(ty: &Type, dependencies: &mut Dependencies, generics: &Generi
let generic_ident = generic.ident.clone();
let generic_ident_str = generic_ident.to_string();

if !generic.bounds.is_empty() || generic.default.is_some() {
if !generic.bounds.is_empty() {
return quote!(#generic_ident_str.to_owned());
}

Expand Down
12 changes: 8 additions & 4 deletions ts-rs/tests/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ fn generic_struct() {
}

#[test]
// https://github.com/Aleph-Alpha/ts-rs/issues/56 TODO
fn inline() {
#[derive(TS)]
struct Generic<T> {
Expand All @@ -173,6 +172,8 @@ fn inline() {
#[test]
#[ignore = "We haven't figured out how to inline generics with bounds yet"]
fn inline_with_bounds() {
todo!("FIX ME: https://github.com/Aleph-Alpha/ts-rs/issues/214");

#[derive(TS)]
struct Generic<T: ToString> {
t: T,
Expand All @@ -181,13 +182,16 @@ fn inline_with_bounds() {
#[derive(TS)]
struct Container {
g: Generic<String>,

#[ts(inline)]
gi: Generic<String>,

#[ts(flatten)]
t: Generic<u32>,
}

assert_eq!(Generic::<&'static str>::decl(), "type Generic<T> = { t: T, }");

Check warning on line 193 in ts-rs/tests/generics.rs

View workflow job for this annotation

GitHub Actions / Test ts-rs

unreachable statement

Check warning on line 193 in ts-rs/tests/generics.rs

View workflow job for this annotation

GitHub Actions / Test ts-rs

unreachable statement
// ^^^^^^^^^^^^ Replace with something else
assert_eq!(
Container::decl(),
"type Container = { g: Generic<string>, gi: { t: string, }, t: number, }"
Expand All @@ -196,7 +200,6 @@ fn inline_with_bounds() {
}

#[test]
#[ignore = "We haven't figured out how to inline generics with defaults yet"]
fn inline_with_default() {
#[derive(TS)]
struct Generic<T = String> {
Expand All @@ -206,17 +209,18 @@ fn inline_with_default() {
#[derive(TS)]
struct Container {
g: Generic<String>,

#[ts(inline)]
gi: Generic<String>,

#[ts(flatten)]
t: Generic<u32>,
}

assert_eq!(Generic::<&'static str>::decl(), "type Generic<T = string> = { t: T, }");
assert_eq!(Generic::<()>::decl(), "type Generic<T = string> = { t: T, }");
assert_eq!(
Container::decl(),
"type Container = { g: Generic<string>, gi: { t: string, }, t: number, }"
// Actual output: { g: Generic<string>, gi: { t: T, }, t: T, }
);
}

Expand Down

0 comments on commit a0a3e13

Please sign in to comment.