Open gluax opened 2 years ago
Is there a reason you have #[wasm_bindgen(skip)]
on the test3
field? It seems like this might be causing your problem, and you don't need this even if you override both the getter and the setter, see the example here: https://rustwasm.github.io/docs/wasm-bindgen/reference/attributes/on-rust-exports/getter-and-setter.html
@trevyn I do have to skip since the type there since IndexMap
, nor do HashMap
, support the into_wasm_abi
traits. Nor does the Value
type from serde
.
We managed to find a way around it by doing the following(though it still feels like this process could be improved):
use indexmap::IndexMap;
use serde_json::Value;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
#[wasm_bindgen(typescript_custom_section)]
const METADATA_TYPE: &'static str = r#"
export type JSMetadata = {
[key in string]: any
};
"#;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "JSMetadata")]
pub type Test3;
}
#[wasm_bindgen(getter_with_clone)]
pub struct Tester {
pub test1: String,
pub test2: u32,
#[wasm_bindgen(skip, typescript_type = "JSMetadata")]
pub test3: IndexMap<String, Value>,
}
#[wasm_bindgen]
impl Tester {
#[wasm_bindgen(catch, constructor, js_class = "Tester")]
pub fn new(test1: String, test2: u32, test3: Option<Test3>) -> Self {
Self {
test1,
test2,
test3: if let Some(t) = test3 {
t.into_serde().unwrap()
} else {
Default::default()
},
}
}
#[wasm_bindgen(getter = test3, typescript_type = "JSMetadata")]
pub fn test3(&self) -> JsValue {
JsValue::from_serde(&self.test3).unwrap()
}
#[wasm_bindgen(setter = test3, typescript_type = "JSMetadata")]
pub fn set_test3(&mut self, field: Test3) {
self.test3 = field.into_serde().unwrap()
}
}
Not sure if doing typescript_type
on all of those instances of wasm_bindgen
is necessary though.
Hi all we are trying to set proper types on getter and setters rather than it just showing as
any
from typescript on the fieldtest3
.However, we can find very little documentation on how this crate actually works despite the guidebook.
The code is as follows:
How do we specify that for the field
test3
it should be of typeJSMetadata
as we defined above?