Skip to content

wasm-bindgen/ts-gen

Repository files navigation

ts-gen

Generate wasm-bindgen Rust bindings from TypeScript .d.ts declaration files.

Overview

ts-gen reads TypeScript .d.ts files and produces Rust source code containing #[wasm_bindgen] extern blocks, ready to compile against wasm-bindgen.

Pipeline

.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

Installation

CLI

cargo install ts-gen

Library

cargo add ts-gen --no-default-features

The cli feature (enabled by default) pulls in clap and walkdir for the binary. Disable it if you only need the library API.

Usage

CLI

ts-gen --input types.d.ts --output src/bindings.rs

Process a directory of declaration files:

ts-gen --input ./typings/ --output src/bindings.rs --lib-name my-js-lib

Map 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"

Library API

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}");

Supported TypeScript constructs

  • 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)

Known limitations

  • Generic type arguments are not yet preserved in codegen; generic types emit their base name only. This will improve as wasm-bindgen generic support matures.
  • The parsed IR uses Rc<str> internally and is !Send / !Sync.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

About

Automated TypeScript Bindgen

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages