Skip to content

Using --embind-emit-tsd should define auxiliary Webassembly-module members #20296

Open
@jnickg

Description

@jnickg

On the latest release of EMSDK, 3.1.46, I can now use the --embind-emit-tsd flag to generate embind TS definitions for my webassembly modules. However, naively switching to this causes some TS compiler complaints about missing members and functions, as our hand-written .d.ts files included some subset of the auxiliary EmscriptenModule definitions (basically whatever we were actually using and needed). For our projects we used a pattern looking like this:

export interface WebassemblyModule {
    HEAPU8: Uint8Array;
    _malloc(size: number): number;
    _free(ptr: number): void;
    // Other auxiliary stuff might be defined here
}

export interface MyModule extends WebassemblyModule {
  myBindingsGoHere(): void;
}

declare function MyModule(options?: {
    locateFile(filename: string): string;
}): Promise<MyModule>;

export default MyModule;

In concept this can be worked around without too much effort, by emitting the embinding definitions to something like wasm.ts, then hard-coding a TS definition file like this.

import {
    type MainModule
} from "./wasm.js";

export interface WebassemblyModule {
    HEAPU8: Uint8Array;
    _malloc(size: number): number;
    _free(ptr: number): void;
}

export type MyRealModule = MainModule & WebassemblyModule;

declare function MyRealModule(options?: {
    locateFile(filename: string): string;
}): Promise<MyRealModule>;

export default MyRealModule;

export * from "./wasm.js";

Of course the downside to this is that it still has to be manually maintained, and it adds complication to builds.

If it's not a huge lift, it would be nice to include auxiliary stuff, such as things exposed by the EXPORTED_FUNCTIONS flag & whatever is always there, so users don't have to manually add them. That way, nothing needs to be manually maintained, and the d.ts file advertises (closer to) everything accessible on the module object.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions