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 會說明是number
。
# #![allow(unused_variables)] #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) {
// ...
}
}