Tauri TypeGen
A command-line tool that automatically generates TypeScript models and bindings from your Tauri commands, eliminating the manual process of creating frontend types and validation.
Features
- 🔍 Automatic Discovery: Scans your Rust source files to find all
#[tauri::command]functions - 📝 TypeScript Generation: Creates TypeScript interfaces for all command parameters and return types
- ✅ Validation Support: Generates validation schemas using Zod or plain TypeScript types
- 🚀 Command Bindings: Creates strongly-typed frontend functions that call your Tauri commands
- 🎯 Type Safety: Ensures frontend and backend types stay in sync
- 🛠️ CLI Integration: Generate types with a simple command:
cargo tauri-typegen generate - 📊 Dependency Visualization: Optional dependency graph generation for complex projects
- ⚙️ Configuration Support: Supports both standalone config files and Tauri project integration
Table of Contents
- Quick Start
- Installation
- Usage Examples
- TypeScript Compatibility
- API Reference
- Configuration Options
- Development
Quick Start
-
Install the CLI tool:
-
Generate TypeScript bindings from your Tauri project:
# In your Tauri project root -
Use the generated bindings in your frontend:
import { createUser, getUsers } from './src/generated'; const user = await createUser({ request: { name: "John", email: "john@example.com" } }); const users = await getUsers({ filter: null });
CLI Commands
Installation
CLI Tool Installation
Install the CLI tool globally:
TypeScript Bindings (Optional)
If you need TypeScript bindings for frontend integration:
Note: This plugin requires manual installation. The cargo tauri add command only works with official Tauri plugins.
Configuration Setup
Initialize Configuration
Add typegen configuration to your existing Tauri project:
# Add configuration to your existing tauri.conf.json (default)
# Specify custom validation library
# Create a standalone config file
Note: The init command requires an existing tauri.conf.json file in your Tauri project. It will update the file to add the plugins.typegen configuration section.
Configuration File
Configuration can be stored in a standalone JSON file or within your tauri.conf.json:
Package.json Integration
Add generation to your build scripts:
Usage Examples
Example: E-commerce App
Let's say you have these Tauri commands in your Rust backend:
src-tauri/src/commands/products.rs:
use ;
use command;
use Validate;
pub async
pub async
pub async
Generate TypeScript Bindings
Command Line Generation
Generate bindings with the CLI tool:
# Basic generation with defaults
# Custom paths and validation
# Generate with dependency visualization
# Use configuration file
# Quick examples for different setups
Build Integration
The recommended approach is to use Tauri's built-in build hooks to ensure types are generated before the frontend build starts. This solves the chicken-and-egg problem where the frontend needs the generated types but builds before the Rust backend.
Method 1: Tauri Build Hooks (Recommended)
First, add configuration to your tauri.conf.json:
Then use standard Tauri commands:
# Development - types generated automatically before frontend starts
# Production build - types generated before frontend build
Method 2: Package.json Scripts (Alternative)
If you prefer explicit control in package.json:
Method 3: Cargo Build Scripts (Advanced)
For tighter integration, add type generation to your Rust build process.
Add tauri-typegen as build dependency to your project.
In src-tauri/build.rs:
Generated Files Structure
After running the generator:
src/generated/
├── types.ts # TypeScript interfaces
├── schemas.ts # Zod validation schemas (if using zod)
├── commands.ts # Typed command functions
├── index.ts # Barrel exports
├── dependency-graph.txt # Text dependency visualization (if --visualize-deps)
└── dependency-graph.dot # DOT format graph (if --visualize-deps)
Generated types.ts:
export interface Product {
id: number;
name: string;
description: string;
price: number;
inStock: boolean;
categoryId: number;
}
export interface CreateProductRequest {
name: string;
description: string;
price: number;
categoryId: number;
}
export interface CreateProductParams {
request: CreateProductRequest;
}
export interface GetProductsParams {
filter?: ProductFilter | null;
}
export interface DeleteProductParams {
id: number;
}
Generated commands.ts:
import { invoke } from '@tauri-apps/api/core';
import * as schemas from './schemas';
import type * as types from './types';
export async function createProduct(params: types.CreateProductParams): Promise<types.Product> {
const validatedParams = schemas.CreateProductParamsSchema.parse(params);
return invoke('create_product', validatedParams);
}
export async function getProducts(params: types.GetProductsParams): Promise<types.Product[]> {
const validatedParams = schemas.GetProductsParamsSchema.parse(params);
return invoke('get_products', validatedParams);
}
export async function deleteProduct(params: types.DeleteProductParams): Promise<void> {
const validatedParams = schemas.DeleteProductParamsSchema.parse(params);
return invoke('delete_product', validatedParams);
}
Using Generated Bindings in Frontend
React Example
import React, { useEffect, useState } from 'react';
import { getProducts, createProduct, deleteProduct } from '../lib/generated';
import type { Product, ProductFilter } from '../lib/generated';
export function ProductList() {
const [products, setProducts] = useState<Product[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
loadProducts();
}, []);
const loadProducts = async () => {
try {
setLoading(true);
const result = await getProducts({ filter: null });
setProducts(result);
} catch (error) {
console.error('Failed to load products:', error);
} finally {
setLoading(false);
}
};
const handleCreateProduct = async () => {
try {
const newProduct = await createProduct({
request: {
name: 'New Product',
description: 'A new product',
price: 19.99,
categoryId: 1,
}
});
setProducts([...products, newProduct]);
} catch (error) {
console.error('Failed to create product:', error);
}
};
const handleDeleteProduct = async (productId: number) => {
try {
await deleteProduct({ id: productId });
setProducts(products.filter(p => p.id !== productId));
} catch (error) {
console.error('Failed to delete product:', error);
}
};
if (loading) return <div>Loading...</div>;
return (
<div>
<h2>Products</h2>
<button onClick={handleCreateProduct}>Create Product</button>
<div className="products">
{products.map((product) => (
<div key={product.id} className="product-card">
<h3>{product.name}</h3>
<p>{product.description}</p>
<p>${product.price}</p>
<p>Stock: {product.inStock ? '✅' : '❌'}</p>
<button onClick={() => handleDeleteProduct(product.id)}>
Delete
</button>
</div>
))}
</div>
</div>
);
}
Vue Example
<template>
<div class="product-manager">
<h2>Product Manager</h2>
<form @submit.prevent="createProduct" class="create-form">
<input v-model="newProduct.name" placeholder="Product name" required />
<textarea v-model="newProduct.description" placeholder="Description"></textarea>
<input v-model.number="newProduct.price" type="number" step="0.01" placeholder="Price" required />
<button type="submit">Create Product</button>
</form>
<div class="products-list">
<div v-for="product in products" :key="product.id" class="product-item">
<h4>{{ product.name }}</h4>
<p>{{ product.description }}</p>
<p>${{ product.price }}</p>
<button @click="deleteProduct(product.id)">Delete</button>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { getProducts, createProduct as createProductCmd, deleteProduct as deleteProductCmd } from '../lib/generated';
import type { Product, CreateProductRequest } from '../lib/generated';
const products = ref<Product[]>([]);
const newProduct = ref<CreateProductRequest>({
name: '',
description: '',
price: 0,
categoryId: 1,
});
onMounted(async () => {
await loadProducts();
});
const loadProducts = async () => {
try {
const result = await getProducts({ filter: null });
products.value = result;
} catch (error) {
console.error('Failed to load products:', error);
}
};
const createProduct = async () => {
try {
const product = await createProductCmd({ request: { ...newProduct.value } });
products.value.push(product);
newProduct.value = { name: '', description: '', price: 0, categoryId: 1 };
} catch (error) {
console.error('Failed to create product:', error);
}
};
const deleteProduct = async (id: number) => {
try {
await deleteProductCmd({ id });
products.value = products.value.filter(p => p.id !== id);
} catch (error) {
console.error('Failed to delete product:', error);
}
};
</script>
Svelte Example
src/lib/ProductStore.ts:
import { writable } from 'svelte/store';
import { getProducts, createProduct, deleteProduct } from './generated';
import type { Product, ProductFilter } from './generated';
export const products = writable<Product[]>([]);
export const loading = writable(false);
export const productStore = {
async loadProducts(filter: ProductFilter = {}) {
loading.set(true);
try {
const result = await getProducts({ filter });
products.set(result);
} catch (error) {
console.error('Failed to load products:', error);
} finally {
loading.set(false);
}
},
async createProduct(request: CreateProductRequest) {
try {
const newProduct = await createProduct({ request });
products.update(items => [...items, newProduct]);
return newProduct;
} catch (error) {
console.error('Failed to create product:', error);
throw error;
}
},
async deleteProduct(id: number) {
try {
await deleteProduct({ id });
products.update(items => items.filter(p => p.id !== id));
} catch (error) {
console.error('Failed to delete product:', error);
throw error;
}
}
};
Benefits of Using Generated Bindings
✅ Type Safety
// ❌ Before: Manual typing, prone to errors
const result = await invoke('create_product', {
name: 'Product',
price: '19.99', // Oops! Should be number
category_id: 1 // Oops! Should be camelCase
});
// ✅ After: Generated bindings with validation
const result = await createProduct({
request: {
name: 'Product',
price: 19.99, // Correct type
categoryId: 1 // Correct naming
}
});
✅ Runtime Validation
// Automatically validates input at runtime
try {
await createProduct({
request: {
name: '', // Will throw validation error
price: -5 // Will throw validation error
}
});
} catch (error) {
console.error('Validation failed:', error);
}
✅ IntelliSense & Autocomplete
Your IDE will provide full autocomplete and type hints for all generated functions and types.
✅ Automatic Updates
When you change your Rust commands, just re-run the generator to get updated TypeScript bindings.
Command Hooks (Zod Only)
When using Zod validation, generated commands support optional lifecycle hooks for side effects like notifications, logging, and analytics.
Hook Interface
export interface CommandHooks<T> {
/** Called when Zod schema validation fails */
onValidationError?: (error: ZodError) => void;
/** Called when Tauri invoke fails (Rust error, serialization, etc.) */
onInvokeError?: (error: unknown) => void;
/** Called when command succeeds */
onSuccess?: (result: T) => void;
/** Called after command settles (success or error) */
onSettled?: () => void;
}
Basic Usage
import { createProduct } from './generated';
import { toast } from 'your-notification-library';
await createProduct(
{ request: productData },
{
onValidationError: (error) => {
toast.error(error.errors[0].message);
},
onInvokeError: (error) => {
toast.error('Failed to create product');
},
onSuccess: (product) => {
toast.success(`Created ${product.name}!`);
},
onSettled: () => {
console.log('Operation completed');
},
}
);
Reusable Hook Patterns
Create reusable hook factories for consistent error handling:
// lib/api-hooks.ts
import { CommandHooks } from './generated';
import { toast } from 'your-notification-library';
import { ZodError } from 'zod';
export function withNotifications<T>(messages?: {
success?: string;
error?: string;
}): CommandHooks<T> {
return {
onValidationError: (error: ZodError) => {
toast.error(error.errors[0].message);
},
onInvokeError: () => {
toast.error(messages?.error || 'Operation failed');
},
onSuccess: () => {
if (messages?.success) {
toast.success(messages.success);
}
},
};
}
// Usage across your app
await createProduct(
{ request: data },
withNotifications({ success: 'Product created!' })
);
await deleteProduct(
{ id: productId },
withNotifications({ success: 'Product deleted!' })
);
React Query Integration
import { useMutation } from '@tanstack/react-query';
import { createProduct } from './generated';
import { toast } from 'sonner';
function useCreateProduct() {
return useMutation({
mutationFn: (data) => createProduct(
{ request: data },
{
onValidationError: (err) => toast.error(err.errors[0].message),
onSuccess: () => toast.success('Product created!'),
}
),
});
}
Hooks Are Optional
Hooks are completely optional - commands work without them:
// Without hooks (backward compatible)
const product = await createProduct({ request: data });
// With partial hooks
const product = await createProduct(
{ request: data },
{ onSuccess: (p) => console.log('Created:', p) }
);
// Traditional try/catch still works
try {
const product = await createProduct({ request: data });
} catch (error) {
console.error(error);
}
Mixing Hooks with Framework Control Flow
Hooks work seamlessly with framework-specific async patterns. Here's a Svelte example using {#await} blocks:
<script lang="ts">
import { createProduct } from './generated';
import { toast } from 'your-notification-library';
let productData = { name: '', price: 0, categoryId: 1 };
// Hooks handle side effects (notifications)
// Svelte's #await handles UI state (loading, success, error)
let productPromise = createProduct(
{ request: productData },
{
onValidationError: (err) => toast.error(err.errors[0].message),
onSuccess: (product) => toast.success(`Created ${product.name}!`),
}
);
</script>
{#await productPromise}
<!-- Loading state -->
<p>Creating product...</p>
{:then product}
<!-- Success state -->
<p>Product created: {product.name}</p>
{:catch error}
<!-- Error state -->
<p>Error: {error.message}</p>
{/await}
This pattern shows how hooks and normal control flow complement each other:
- Hooks = Cross-cutting concerns (notifications, analytics, logging)
- await/catch/UI state = Primary application logic and user feedback
Note: Hooks are synchronous side effects that execute during the command lifecycle. Errors are always re-thrown after hooks execute to preserve normal control flow. If a hook itself throws an error, it will propagate and terminate the command - hooks are not wrapped in try-catch.
TypeScript Compatibility
The generated TypeScript code is compatible with modern TypeScript environments and follows current best practices.
Version Requirements
- TypeScript 3.7+ (for optional chaining support)
- ES2018+ compilation target
- Zod 3.x (when using Zod validation)
Generated Code Features
The generated TypeScript code uses modern language features:
- ES Modules:
import/exportstatements - Async/Await: All command functions are async
- Union Types:
string | null, optional properties - Generic Types:
Array<T>,Promise<T>,Record<K, V> - Tuple Types:
[string, number]for Rust tuples - Template Literal Types: Advanced string manipulation (when needed)
TypeScript Configuration
Ensure your tsconfig.json is compatible with the generated code:
Generated Type Mappings
| Rust Type | Generated TypeScript | Notes |
|---|---|---|
String, &str |
string |
Basic string types |
i32, f64, etc. |
number |
All numeric types → number |
bool |
boolean |
Boolean type |
() |
void |
Unit type |
Option<T> |
T | null |
Nullable types |
Vec<T> |
T[] |
Arrays |
HashMap<K, V> |
Map<K, V> |
Map type |
BTreeMap<K, V> |
Map<K, V> |
Consistent with HashMap |
HashSet<T> |
T[] |
Arrays for JSON compatibility |
(T, U) |
[T, U] |
Tuple types |
Result<T, E> |
T |
Errors handled by Tauri runtime |
API Reference
CLI Commands
Library Usage (Advanced)
For programmatic usage in build scripts:
use ;
let config = GenerateConfig ;
let files = generate_from_config?;
Configuration Options
Validation Libraries
zod- Generates Zod schemas with validationnone- No validation schemas generated, only TypeScript types
Example Project Structure
my-tauri-app/
├── src-tauri/
│ ├── src/
│ │ ├── commands/
│ │ │ ├── user.rs # Contains #[command] functions
│ │ │ └── mod.rs
│ │ └── lib.rs
│ └── Cargo.toml
├── src/
│ ├── generated/ # Generated by this plugin
│ │ ├── types.ts
│ │ ├── schemas.ts
│ │ ├── commands.ts
│ │ └── index.ts
│ └── App.tsx
└── package.json
Development
Building the Plugin
Running Tests
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
License
This project is licensed under the MIT License.