Expand description
§Tauri TypeGen
Automatically generate TypeScript bindings from Tauri commands.
This library scans Rust source code for #[tauri::command] functions and generates
strongly-typed TypeScript interfaces, command functions, and optional Zod schemas
with runtime validation.
§Features
- 🔍 Automatic Discovery: Scans Rust source for
#[tauri::command]functions - 📝 TypeScript Generation: Creates TypeScript interfaces for command parameters and return types
- ✅ Validation Support: Optional Zod schema generation with runtime validation
- 🚀 Command Bindings: Strongly-typed frontend functions
- 📡 Event Support: Discovers and types
app.emit()events - 📞 Channel Support: Types for streaming
Channel<T>parameters - 🏷️ Serde Support: Respects
#[serde(rename)]and#[serde(rename_all)]attributes
§Quick Start
§As a CLI Tool
# Install globally
cargo install tauri-typegen
# Generate TypeScript bindings
cargo tauri-typegen generate§As a Build Dependency
Add to your src-tauri/build.rs:
ⓘ
fn main() {
// Generate TypeScript bindings before build
tauri_typegen::BuildSystem::generate_at_build_time()
.expect("Failed to generate TypeScript bindings");
tauri_build::build()
}§Programmatic Usage
use tauri_typegen::{GenerateConfig, generate_from_config};
let config = GenerateConfig {
project_path: "./src-tauri".to_string(),
output_path: "./src/generated".to_string(),
validation_library: "zod".to_string(),
verbose: Some(true),
..Default::default()
};
let files = generate_from_config(&config)?;§Example
Given this Rust code:
ⓘ
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct User {
pub id: i32,
pub name: String,
}
#[tauri::command]
pub async fn get_user(id: i32) -> Result<User, String> {
// Implementation
}Generates this TypeScript:
export interface User {
id: number;
name: string;
}
export async function getUser(params: { id: number }): Promise<User> {
return invoke('get_user', params);
}§Configuration
Configure via tauri.conf.json:
{
"plugins": {
"tauri-typegen": {
"project_path": ".",
"output_path": "../src/generated",
"validation_library": "zod",
"type_mappings": {
"DateTime<Utc>": "string",
"PathBuf": "string"
}
}
}
}Re-exports§
pub use interface::config::GenerateConfig;pub use interface::generate_from_config;pub use interface::output::Logger;pub use interface::output::ProgressReporter;pub use build::BuildSystem;pub use models::*;
Modules§
Macros§
- template
- Macro to reduce boilerplate for template registration