Aleph-Alpha/ts-rs

exporting generic type with do_export!()

Closed this issue · 6 comments

Hi! I manually export my structs so that I can create an index.ts barrel file and re-export generated types for those structs within my rust package. I achieve this with do_export!() macro where I first export every struct and then at last create my barrel file.

This method works perfectly fine for normal structs. The problem occurs with generic structs that expect a type argument to be passed to them. Here's an example,

struct StructA {
  prop: String,
}

do_export!(StructA); // good & works great.

struct StructB<T> {
  prop: T,
}

do_export!(StructB); // eh! doesn't work anymore since it expects an arguements

I know ts-rs supports exporting generic structs but are there any solutions for exporting generic structs with do_export!() macro?

I don't think this macro is in the crate anymore

Confused about this as well. I don't think we ever had a macro do_export!, so I think this is something @is-it-ayush implemented?

Maybe he means the old export!?

But I've read some other issues and that doesn't seem like it's the correct syntax for it either. From what I could gather it used to be

export!(Type => "path")

Either way, seems like @is-it-ayush is trying to do the same as #133

I apologise!! this is my oopsie. The macro simply called the ::export() on the type. Here's the macro definition, I forget that I had found this macro from somewhere else and started associating this with a macro provided by ts-rs crate.

macro_rules! do_export {
    ($a:ty) => {{
        match <$a>::export() {
            Ok(()) => {
                println!("✅ {}", <$a>::name());
            }
            Err(e) => {
                println!("❌ {} failed: {}", <$a>::name(), e);
            }
        }
    }};
}

In terms of this issue. I ended up writing a bash script that looped over the directory files ending with .ts and created a new index.ts with all the exports within it. Here's the shell script code.