Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashMap<_, u32> is Record not Map #200

Open
HHogg opened this issue Oct 1, 2024 · 4 comments
Open

HashMap<_, u32> is Record not Map #200

HHogg opened this issue Oct 1, 2024 · 4 comments

Comments

@HHogg
Copy link

HHogg commented Oct 1, 2024

Hi, I have something similar to the rust code below. What I'm seeing at runtime compared to the typeshare generated types don't match up. Is this supported? is there an annotation that needs to be used for this? or a bug?

#[derive(Hash, Serialize)]
#[typeshare]
#[serde(tag = "type", content = "index")]
pub enum Foo {
  Bar,
  Bing,
  Bang(u32),
}


#[derive(Serialize)]
#[typeshare]
pub struct FooParent {
  foos: HashMap<Foo, u32>;
}

wasm-bindgen returns

export interface FooParent { 
  foos: Map<Foo, number> 
};

typeshare returns

export interface FooParent { 
  foos: Record<Foo, number>;
}
@HHogg
Copy link
Author

HHogg commented Oct 3, 2024

By using serialized_as = "Map<Foo, u32>")

#[derive(Serialize)]
#[typeshare]
pub struct FooParent {
  #[typeshare(serialized_as = "Map<Foo, u32>")]
  foos: HashMap<Foo, u32>;
}

The types can be generated like

export interface FooParent { 
  foos: Map<Foo, number> 
};

@kjvalencik
Copy link
Member

serialized_as is a good solution to the problem if you need Map. typeshare is primarily written with JSON serialization in mind. A JSON object is defined as Record<K, V> in TypeScript. Map refers to the Map class.

@HHogg
Copy link
Author

HHogg commented Oct 3, 2024

@kjvalencik Thanks for the reply. I was also looking into the option of serializing as an object, by stringifying the complex key type, but I couldn't get that to work as in the serializer it needed the key to have a static &str life time. Maybe there is a way to do it, but I couldn't work it out...

However the Map type is just as useful to me in the code so I went back to changing typeshare and got it working like the above. I guess I have 2 questions:

  • Should the Typeshare's result match up with the default behaviour of Bindgen?
  • How would I generate a Record<K, V> from a HashMap?

@kjvalencik
Copy link
Member

kjvalencik commented Oct 3, 2024

Should the Typeshare's result match up with the default behaviour of Bindgen?

The default is unlikely to change. The default align with JSON instead of wasm-bindgen. It matches what serde_json + JSON.parse produces.

How would I generate a Record<K, V> from a HashMap?

Configure serde_wasm_bindgen to use .serialize_maps_as_objects(true).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants