From 9f38240017660509f135f8968b59663c3bf20ff2 Mon Sep 17 00:00:00 2001 From: darthturska Date: Sun, 10 Dec 2023 15:00:35 +0200 Subject: [PATCH] fix: Include borrowed type's type arguments in typescript --- macros/src/types/generics.rs | 7 +++++-- ts-rs/tests/lifetimes.rs | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/macros/src/types/generics.rs b/macros/src/types/generics.rs index 290659e32..cea3e3cd1 100644 --- a/macros/src/types/generics.rs +++ b/macros/src/types/generics.rs @@ -1,7 +1,8 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote}; use syn::{ - GenericArgument, GenericParam, Generics, ItemStruct, PathArguments, Type, TypeGroup, TypeTuple, + GenericArgument, GenericParam, Generics, ItemStruct, PathArguments, Type, TypeGroup, + TypeReference, TypeTuple, }; use crate::{attr::StructAttr, deps::Dependencies}; @@ -109,7 +110,9 @@ pub fn format_type(ty: &Type, dependencies: &mut Dependencies, generics: &Generi fn extract_type_args(ty: &Type) -> Option> { let last_segment = match ty { - Type::Group(TypeGroup { elem, .. }) => return extract_type_args(elem), + Type::Group(TypeGroup { elem, .. }) | Type::Reference(TypeReference { elem, .. }) => { + return extract_type_args(elem) + } Type::Path(type_path) => type_path.path.segments.last(), _ => None, }?; diff --git a/ts-rs/tests/lifetimes.rs b/ts-rs/tests/lifetimes.rs index 75bdb64f4..835610e96 100644 --- a/ts-rs/tests/lifetimes.rs +++ b/ts-rs/tests/lifetimes.rs @@ -10,3 +10,25 @@ fn contains_borrow() { assert_eq!(S::decl(), "interface S { s: string, }") } + +#[test] +fn contains_borrow_type_args() { + #[derive(TS)] + #[allow(dead_code)] + struct B<'a, T: 'a> { + a: &'a T, + } + + #[derive(TS)] + #[allow(dead_code)] + struct A<'a> { + a: &'a &'a &'a Vec, //Multiple References + b: &'a Vec>, //Nesting + c: &'a std::collections::HashMap, //Multiple type args + } + + assert_eq!( + A::decl(), + "interface A { a: Array, b: Array>, c: Record, }" + ); +}