Skip to content

Commit

Permalink
Fix top level #[ts(as = "...")] dependency list (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-shigueo authored Mar 6, 2025
1 parent ccb1b14 commit 594fa30
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Fixes
- Fix `#[ts(optional)]` error when using a type alias for `Option` or fully qqualifying it as `core::option::Option` ([#366](https://github.com/Aleph-Alpha/ts-rs/pull/366))
- Fix missing import statements when using `#[ts(as = "...")]` at the top level of a struct/enum ([#385](https://github.com/Aleph-Alpha/ts-rs/pull/385))

# 10.1.0
### Features
Expand Down
14 changes: 10 additions & 4 deletions macros/src/types/type_as.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ use crate::{
pub(crate) fn type_as_struct(attr: &StructAttr, name: &str, type_as: &Type) -> Result<DerivedTS> {
let crate_rename = attr.crate_rename();

let mut dependencies = Dependencies::new(crate_rename.clone());
dependencies.append_from(type_as);

Ok(DerivedTS {
crate_rename: crate_rename.clone(),
crate_rename,
inline: quote!(#type_as::inline()),
inline_flattened: None,
docs: attr.docs.clone(),
dependencies: Dependencies::new(crate_rename),
dependencies,
export: attr.export,
export_to: attr.export_to.clone(),
ts_name: name.to_owned(),
Expand All @@ -27,12 +30,15 @@ pub(crate) fn type_as_struct(attr: &StructAttr, name: &str, type_as: &Type) -> R
pub(crate) fn type_as_enum(attr: &EnumAttr, name: &str, type_as: &Type) -> Result<DerivedTS> {
let crate_rename = attr.crate_rename();

let mut dependencies = Dependencies::new(crate_rename.clone());
dependencies.append_from(type_as);

Ok(DerivedTS {
crate_rename: crate_rename.clone(),
crate_rename,
inline: quote!(#type_as::inline()),
inline_flattened: None,
docs: attr.docs.clone(),
dependencies: Dependencies::new(crate_rename),
dependencies,
export: attr.export,
export_to: attr.export_to.clone(),
ts_name: name.to_owned(),
Expand Down
32 changes: 32 additions & 0 deletions ts-rs/tests/integration/top_level_type_as.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "serde-json-impl")]
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use ts_rs::TS;

#[derive(TS)]
Expand All @@ -20,3 +23,32 @@ pub struct Wrapper<T: TS>(T);
pub fn top_level_type_as_struct() {
assert_eq!(Wrapper::<String>::inline(), r#"string"#)
}

#[cfg(feature = "serde-json-impl")]
#[derive(TS)]
#[ts(
export,
export_to = "top_level_type_as/",
as = "HashMap::<String, JsonValue>"
)]
pub struct JsonMap(JsonValue);

#[derive(TS)]
#[ts(export, export_to = "top_level_type_as/")]
pub struct Foo {
x: i32,
}

#[derive(TS)]
#[ts(export, export_to = "top_level_type_as/")]
pub struct Bar {
foo: Foo,
}

#[derive(TS)]
#[ts(
export,
export_to = "top_level_type_as/",
as = "HashMap::<String, Bar>"
)]
pub struct Biz(String);

0 comments on commit 594fa30

Please sign in to comment.