tauri-ts-generator
CLI tool to automatically generate TypeScript bindings from Tauri commands.
Description
tauri-ts-generator scans your Rust code, finds all functions with the #[tauri::command] macro, and automatically generates:
- TypeScript interfaces for Rust structs and enums
- TypeScript wrapper functions to invoke Tauri commands via
invoke()
This ensures type-safe interaction between the TypeScript frontend and Rust backend, minimizing manual boilerplate.
Installation
Or build from source:
Quick Start
1. Initialize Configuration
This will create a tauri-codegen.toml file with default settings.
2. Configure Settings
Edit tauri-codegen.toml:
[]
= "src-tauri/src"
= ["tests", "target"]
[]
= "src/generated/types.ts"
= "src/generated/commands.ts"
[]
= ""
= ""
= ""
= ""
3. Generate TypeScript Code
Or with verbose output:
Example
Rust Code (Input)
use ;
pub async
pub async
TypeScript Code (Output)
types.ts:
// This file was auto-generated by tauri-ts-generator
// Do not edit this file manually
export interface User {
id: number;
name: string;
email: string | null;
}
export type UserStatus = "Active" | "Inactive" | "Pending";
commands.ts:
// This file was auto-generated by tauri-ts-generator
// Do not edit this file manually
import { invoke } from "@tauri-apps/api/core";
import type { User } from "./types";
export async function getUser(id: number): Promise<User> {
return invoke<User>("get_user", { id });
}
export async function getAllUsers(): Promise<User[]> {
return invoke<User[]>("get_all_users");
}
export async function greet(name: string): Promise<string> {
return invoke<string>("greet", { name });
}
Type Mapping
Basic Types
| Rust | TypeScript |
|---|---|
String, &str, char |
string |
i8, i16, i32, i64, u8, u16, u32, u64 |
number |
f32, f64 |
number |
bool |
boolean |
Vec<T> |
T[] |
Option<T> |
T | null |
Result<T, E> |
Promise<T> |
HashMap<K, V> |
Record<K, V> |
() |
void |
| Custom struct | TypeScript interface |
| Simple enum | String union type |
External Types (Crates)
| Rust | TypeScript | Description |
|---|---|---|
chrono::DateTime<Tz> |
string |
ISO 8601 format |
chrono::NaiveDateTime |
string |
ISO 8601 format |
chrono::NaiveDate |
string |
YYYY-MM-DD |
chrono::NaiveTime |
string |
HH:MM:SS |
time::OffsetDateTime |
string |
ISO 8601 format |
time::PrimitiveDateTime |
string |
ISO 8601 format |
uuid::Uuid |
string |
UUID string |
rust_decimal::Decimal |
string |
Numeric string |
std::path::PathBuf |
string |
File path |
url::Url |
string |
URL string |
std::net::IpAddr |
string |
IP address |
std::time::Duration |
number |
Seconds |
serde_json::Value |
unknown |
Any JSON |
bytes::Bytes |
number[] |
Byte array |
| Complex enum | Discriminated union |
Configuration
[input]
| Parameter | Description | Default |
|---|---|---|
source_dir |
Directory with Rust source files | src-tauri/src |
exclude |
List of directories/files to exclude | ["tests", "target"] |
[output]
| Parameter | Description | Default |
|---|---|---|
types_file |
Path to generate TypeScript types | src/generated/types.ts |
commands_file |
Path to generate TypeScript commands | src/generated/commands.ts |
[naming]
| Parameter | Description | Default |
|---|---|---|
type_prefix |
Prefix for type names | "" |
type_suffix |
Suffix for type names | "" |
function_prefix |
Prefix for function names | "" |
function_suffix |
Suffix for function names | "" |
CLI Commands
)
generate
init
Features
- Automated Parsing — uses
synto analyze Rust AST - Async Support — correctly handles
asynccommands - Custom Types — parses structs and enums with
#[derive(Serialize)]attributes - Serde Rename — respects
#[serde(rename = "...")] - Tauri Special Types — automatically skips
State,Window,AppHandle - Nested Modules — scans commands inside
modblocks
License
MIT