Add a TypeScript definition file
radu-matei opened this issue · comments
First of all, thank you for this really great module!
Trying to use this with TypeScript, it requires a definition file for the types and functions exported.
The definition file below follows the Mozilla docs for instantiate
, instantiateStreaming
, and the definitions from TypeScript:
declare module "asyncify-wasm" {
class Instance extends WebAssembly.Instance {
constructor(module: WebAssembly.Module, imports: WebAssembly.Imports);
}
function instantiate(
bytes: BufferSource,
importObject?: WebAssembly.Imports
): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
function instantiate(
moduleObject: WebAssembly.Module,
importObject?: WebAssembly.Imports
): Promise<WebAssembly.Instance>;
function instantiateStreaming(
response: Response | PromiseLike<Response>,
importObject?: WebAssembly.Imports
): Promise<WebAssembly.WebAssemblyInstantiatedSource>;
}
Having the above in index.d.ts
at the root of the module satisfies the compiler checks.
I have a branch with this change that I'd be happy to contribute upstream.
That being said, including the definitions in the module means they will have to be kept in sync if the public API of the module changes - and I understand if this is out of scope for the project.
(If this ends up not being part of the project, the tsconfig
file for a TypeScript project can be modified to include
the path to a definition file with the above contents.)
Thanks!
@radu-matei Oh hi! Sorry I missed your issue, subscribed to the repo now to avoid this happening again.
I do agree TS definitions would be useful, and it's something I had to work around myself. I'll happily accept a PR!
In the future, we might try to add JSDoc comments and autogenerate definitions, but, on the other hand, this API is meant to be intentionally stable to match the WebAssembly
one, so I think this would be an overkill.
For now, a PR with your definitions would be more than enough!
Note that another thing you could do to avoid having to repeat params / results definitions is to declare functions as const
+ typeof
instead, e.g.:
const instantiate: typeof WebAssembly.instantiate;