tsync icon indicating copy to clipboard operation
tsync copied to clipboard

`HashMap` value as `any` type

Open paydii opened this issue 1 year ago • 2 comments

Hey, thanks for this. It's been really useful.

I'm trying to convert a dynamic metadata field that will be unkown of type as the value, but the key will always be a string.

#[derive(Serialize, Deserialize, Debug)]
#[tsync]
pub struct Transaction {
    pub id: i32,
    pub amount: i32,
    pub formatted_amount: String,
    pub transaction_type: String,
    pub description: String,
    pub balance_after: i32,
    pub reference: Option<TransactionReference>,
    pub metadata: Option<HashMap<String, String>>,
    pub created_at: String,
    pub credit: bool,
    pub debit: bool,
}

I found this issue but not sure if it explains my case. What I tried are change the type to Option<HashMap<String, Value>> and pub metadata: Option<Value>, to no succeed. They were generated as metadata?: Value; which is unkown type.

Is this possible at all?

paydii avatar Aug 28 '25 10:08 paydii

this might be possible if/when #34 is implemented, but until then would defining Value as an alias to any in your typescript do the trick?

AnthonyMichaelTDM avatar Oct 30 '25 23:10 AnthonyMichaelTDM

My suggestion is to introduce a Rust alias for this

type SomeAlias = String

#[tsync]
pub struct Example {
    pub meta: Option<HashMap<String, SomeAlias>>,
}

and in your typescript, add the following (in a global types file)

type SomeAlias = any

That way you don't have to use Value in your HashMap on the Rust side.

You can also use a custom type when #73 is merged in, but (personally) I'd opt for the alias here so future refactors maintain integrity without having to manually update the #[tsync(ts_type='...') string in the attribute.

Wulf avatar Dec 23 '25 04:12 Wulf