Macro to derive wasm-bindgen Typescript definitons from structs.
export_ts_macro={ git = "https://github.com/ivanschuetz/export_ts_macro", branch = "main" }
use export_ts_macro::export_ts;
use wasm_bindgen::prelude::*;
#[export_ts(FooTs)]
struct Foo {
a: i32,
b: String,
}
#[wasm_bindgen]
pub fn hello(pars: FooTs) {
let js: JsValue = pars.into();
let _actual: Foo = js.into_serde().unwrap();
}
This generates the following Typescript:
interface FooTs {
a: i32;
b: string;
}
/**
* @param {FooTs} pars
*/
export function foo(pars: FooTs): void;
The macro generates code according to the typescript_type section in the wasm-bindgen guide.
#[wasm_bindgen(typescript_custom_section)]
const FooTs: &'static str = r#"
interface FooTs {
a: number
b: string
}
"#;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "FooTs")]
pub type FooTs;
}