TypeScript:此表达式不可调用。 'ExportValue' 类型的成分不可调用。 WebAssembly 模块

TypeScript: This expression is not callable. No constituent of type 'ExportValue' is callable. WebAssembly module

我在 index.ts 中使用 Deno 运行 这段代码时遇到了这个问题。

const wasm = await Deno.readFile("./wasm_test/pkg/wasm_test_bg.wasm");
const wasmModule = new WebAssembly.Module(wasm);
const wasmInstance = new WebAssembly.Instance(wasmModule);
const wasmTest = wasmInstance.exports;
wasmTest.sum(1, 3); // Error

Error: This expression is not callable. No constituent of type 'ExportValue' is callable.

调用 sum 时出现错误,结果应该是 4。 当我 运行 它作为 index.js 它完美地工作。 我使用 wasm-pack 来编译 Rust 代码。

问题是名字 add 未知。 如下更改代码的第 4 行:

const wasm = await Deno.readFile("./add.wasm");
const wasmModule = new WebAssembly.Module(wasm);
const wasmInstance = new WebAssembly.Instance(wasmModule);
const sum = wasmInstance.exports.sum as CallableFunction; // exports.add if you test with the below linked wasm.
console.log(sum(1, 3))

documentation

对于我的测试,我找到了一个带有 add-函数 here 的 wasm 示例。