Generate wasm-bindgen Rust bindings from TypeScript .d.ts declaration files.
ts-gen reads TypeScript .d.ts files and produces Rust source code containing #[wasm_bindgen] extern blocks, ready to compile against wasm-bindgen.
.d.ts files
-> oxc_parser (AST)
-> First Pass phase 1: collect all type names into TypeRegistry
-> First Pass phase 2: populate full IR (resolve references, merge var+interface patterns)
-> Assembly: group by ModuleContext, resolve inheritance chains, classify types
-> Code Generation: IR -> syn::File -> prettyplease -> .rs files
cargo install ts-gencargo add ts-gen --no-default-featuresThe cli feature (enabled by default) pulls in clap and walkdir for the
binary. Disable it if you only need the library API.
ts-gen --input types.d.ts --output src/bindings.rsProcess a directory of declaration files:
ts-gen --input ./typings/ --output src/bindings.rs --lib-name my-js-libMap external types from other crates:
ts-gen --input types.d.ts --output src/bindings.rs \
--external "Blob=::web_sys::Blob" \
--external "node:buffer=node_buffer_sys"use ts_gen::{parse_source, codegen};
let source = r#"
export declare class Greeter {
constructor(name: string);
greet(): string;
}
"#;
let (module, gctx) = parse_source(source, Some("my-lib"))?;
let rust_code = codegen::generate(&module, &gctx)?;
println!("{rust_code}");- Classes (including abstract classes and inheritance)
- Interfaces (classified as class-like or dictionary types)
- Functions and variables
- String and numeric enums
- Type aliases
- Namespaces
- Module declarations
- Generics (partial -- type parameters are erased to their base types)
- Generic type arguments are not yet preserved in codegen; generic types
emit their base name only. This will improve as
wasm-bindgengeneric support matures. - The parsed IR uses
Rc<str>internally and is!Send/!Sync.
Licensed under either of Apache License, Version 2.0 or MIT license at your option.