ts-macro
ts-macro is a Rust procedural macro crate designed to generate TypeScript
interface bindings for Rust structs. This crate simplifies the process of
creating seamless TypeScript bindings for Rust code, making it easier to work
with WebAssembly and JavaScript ecosystems.

Features
- TypeScript Interface Generation: Automatically generates TypeScript interfaces for Rust structs with camelCase field names and corresponding TypeScript types.
- wasm-bindgen & js-sys Support: Designed for use with
wasm-bindgenprojects and supports all JavaScript standard, built-in objects viajs-sys. - No Boilerplate: Just add the
#[ts]attribute to your struct and get TypeScript interfaces and field getter bindings—no manual parsing required. - Customizable Bindings: Customize TypeScript field names, types, and optionality using attributes.
- Nested Struct Support: Supports nested structs by applying the
tsattribute to each struct individually. - Extensible Interfaces: Extend TypeScript interfaces with other interfaces
using the
extendsargument.
Example
This generates the following TypeScript interface:
interface IToken {
symbol: string;
/**
* @default 18
*/
decimals?: number | undefined;
totalSupply: bigint;
}
Nested Structs
Customizing Bindings
You can customize the TypeScript interface using the ts attribute:
This generates:
interface IParams {
specialCASING: string;
specialFormat: `0x${string}`;
optionalFieldAndValue?: string | undefined;
optionalValue: string | undefined;
optionalField?: string;
}
Extending Interfaces
This generates:
interface JsToken {
// ...
}
interface JsShareToken extends JsToken {
// ...
}
Similar Projects
ts-rs– Generates TypeScript definition files from Rust structs and enums, but isn’t integrated with wasm-bindgen and doesn’t include bindings to parse the type from a JS value.tauri-specta– Solves the same problem and more, but is specifically for Tauri app backends.tsify– Same idea, but is built onserde-wasm-bindgenwhich doesn’t parse JS types, so API types are limited.
Installation
Add the following to your Cargo.toml:
[]
= "0.1.0"
License
This project is licensed under the MIT License.