unchecked_return_typeunchecked_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) {
        // ...
    }
}