unchecked_return_type
和 unchecked_param_type
導出函式和方法的返回和參數型別可以使用 #[wasm_bindgen(unchecked_return_type)]
和 #[wasm_bindgen(unchecked_param_type)]
覆寫。
注意:使用
#[wasm_bindgen(unchecked_return_type)]
和#[wasm_bindgen(unchecked_param_type)]
提供的型別不會檢查其內容。它們將以完全指定的格式出現在函式簽名和 JSDoc 中。例如,在返回String
的函式上使用#[wasm_bindgen(unchecked_return_type = "number")]
將返回一個string
,而不是number
,即使 TS 簽名和 JSDoc 會說否。
#![allow(unused)] fn main() { #[wasm_bindgen(unchecked_return_type = "Foo")] pub fn foo( #[wasm_bindgen(unchecked_param_type = "Bar")] arg1: JsValue, ) -> JsValue { // function body } #[wasm_bindgen] pub struct Foo { // properties } #[wasm_bindgen] impl Foo { #[wasm_bindgen(unchecked_return_type = "Baz")] pub fn foo( &self, #[wasm_bindgen(unchecked_param_type = "Bar")] arg1: JsValue, ) -> JsValue { // function body } } }
這將生成以下 JS 綁定
/**
* @param {Bar} arg1
* @returns {Foo}
*/
export function foo(arg1) {
// ...
}
export class Foo {
/**
* @param {Bar} arg1
* @returns {Baz}
*/
foo(arg1) {
// ...
}
}