โจ typescript-macros
Rusty TypeScript Procedural Macro Library ๐ฆ๐ฎ
๐ Introduction
typescript-macros is the procedural macro library for Rusty TypeScript, providing compile-time code generation capabilities to simplify TypeScript-related Rust code writing.
โจ Implemented Features
| Feature | Description | Status |
|---|---|---|
| ๐๏ธ TypescriptClass | Generate TypeScript class definitions from Rust structs | โ Implemented |
| ๐ TypescriptFunction | Generate TypeScript function type definitions from Rust functions | โ Implemented |
โจ Planned Features
| Feature | Description | Status |
|---|---|---|
| ๐ฎ ts_bindgen | Generate Rust bindings from TS types | ๐ง Planned |
| ๐จ ts_embed | Embed TS code in Rust | ๐ง Planned |
| ๐ ts_function | Auto-generate TS function wrappers | ๐ง Planned |
| ๐ฆ ts_module | Module export macros | ๐ง Planned |
| ๐งช ts_test | TypeScript testing macros | ๐ง Planned |
๐ Quick Start
Add Dependency
[]
= { = "../compilers/typescript-macros" }
ts_bindgen - Type Binding Generation
use ts_bindgen;
// Generate Rust struct from TypeScript interface
ts_embed - Embed TypeScript
use ts_embed;
use TypeScript;
TypescriptClass - Generate TypeScript Class Definition
use TypescriptClass;
TypescriptFunction - Generate TypeScript Function Type Definition
use TypescriptFunction;
ts_function - Function Export
use ts_function;
use TypeScript;
;
๐ฆ Macro Details
๐ฎ #[ts_bindgen]
Generates Rust code from TypeScript type definitions.
Supported Types
| TypeScript | Rust |
|---|---|
number |
f64 |
string |
String |
boolean |
bool |
T[] |
Vec<T> |
T | null |
Option<T> |
{ [k: string]: T } |
HashMap<String, T> |
interface |
struct |
type |
type alias |
Example
use ts_bindgen;
๐จ ts_embed!
Embeds TypeScript code at compile time.
use ts_embed;
// Compile-time execution
const RESULT: f64 = ts_embed!;
// RESULT = 4
// Multi-line code
const GREETING: &str = ts_embed! ;
๐ #[ts_function]
Exports Rust functions to the TypeScript runtime.
use ;
๐ ๏ธ Development Guide
Macro Development Process
use TokenStream;
use quote;
use ;
Debugging Macros
# View macro expansion
# Debug mode
|
๐งช Testing
# Run macro tests
# Test macro expansion
# UI tests (compile error checking)
๐ Dependencies
- proc-macro2 - Token processing
- quote - Code generation
- syn - Rust code parsing
- typescript-types - Type system (dev)
- typescript - Runtime (dev)
๐ค Contributing
We welcome issues and PRs! Please ensure:
- โ
Macro code passes
cargo clippychecks - โ Sufficient test cases are added
- โ UI tests cover error scenarios
- โ Documentation includes usage examples
๐ License
MIT License - see LICENSE
๐ฆ Macro magic, making code more concise โจ