I am working on a web app with the following architecture:
- The frontend is a React app written in TypeScript.
- The "backend" is written in Go, and compiled to WASM for distribution with the frontend. (The WASM is for performance reasons.)
The Go documentation on WASM say to use Go's JS support file, which I used in my HTML-only prototype of this app, in <script src="wasm_exec.js"></script> and to call it with
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
});
in a <script> in the HTML.
However, now that I've ported the prototype frontend to React, I'm hitting the following snags:
- The provided
wasm_exec.jsis an anonymous function that runs on inclusion in the HTML page. This function just dumps everything intoglobalThis(which includes theGoclass, which is what I need to access). - Attempting to access
globalThisfrom TypeScript gives aElement implicitly has an 'any' type because type 'typeof globalThis' has no index signature.ts(7017). - I'm seeing some places that say to use a
.d.tstodeclare global {}but then TypeScript says it can't be declared in a module.
How can I trigger my Go-compiled-to-WASM from my TypeScript/React?