wasm-bindgen icon indicating copy to clipboard operation
wasm-bindgen copied to clipboard

Generating TypeScript interfaces for extern "C" types

Open noahbkim opened this issue 1 month ago • 0 comments

To preface, apologies if this question has been asked in some other form. I've done my best to look through the issues here and in wasm-pack but I can't seem to find anything related.

wasm-bindgen provides a nifty API for duck-typing JavaScript objects. Here's the repo's canonical example. When you run e.g. wasm-pack build in examples/duck-typed-interfaces (ignoring the fact that the optimization pass doesn't work for some reason), you get the following TypeScript:

export function make_em_quack_to_this(duck: any): void;

I would like wasm-bindgen to also generate the following interface:

export interface Quacks {
    quack(): string;    
}

This way, the generated TypeScript can be narrowed to:

export function make_em_quack_to_this(duck: Quacks): void;

From the docs, it seems like this is possible by explicitly specifying the entire interface via typescript_type, but doing so feels redundant and easy to forget. Intuitively, I suspect that all the information needed to generate the interface is known at codegen time, but I'm not familiar enough with wasm-bindgen to know why this isn't supported/desired/possible. I'd be happy to put up a PR if this makes sense (and I'd greatly appreciate any suggestions as to where to start looking). Thanks!

noahbkim avatar Dec 02 '25 03:12 noahbkim