You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use specta::Type;use serde::{Deserialize,Serialize};// The `specta::Type` macro allows us to understand your types// We implement `specta::Type` on primitive types for you.// If you want to use a type from an external crate you may need to enable the feature on Specta.#[derive(Serialize,Type)]pubstructMyCustomReturnType{pubsome_field:String,}#[derive(Deserialize,Type)]pubstructMyCustomArgumentType{pubfoo:String,pubbar:i32,}
Annotate your Tauri commands with Specta
#[tauri::command]#[specta::specta]// <-- This bit herefngreet3() -> MyCustomReturnType{MyCustomReturnType{some_field:"Hello World".into(),}}#[tauri::command]#[specta::specta]// <-- This bit herefngreet(name:String) -> String{format!("Hello {name}!")}
Export your bindings
use specta::collect_types;use tauri_specta::{ts, js};// this example exports your types on startup when in debug mode or in a unit test. You can do whatever.fnmain(){#[cfg(debug_assertions)]
ts::export(collect_types![greet, greet2, greet3],"../src/bindings.ts").unwrap();// or export to JS with JSDoc#[cfg(debug_assertions)]
js::export(collect_types![greet, greet2, greet3],"../src/bindings.js").unwrap();}#[test]fnexport_bindings(){
ts::export(collect_types![greet, greet2, greet3],"../src/bindings.ts").unwrap();
js::export(collect_types![greet, greet2, greet3],"../src/bindings.js").unwrap();}
Usage on frontend
import*ascommandsfrom"./bindings";// This should point to the file we export from Rustawaitcommands.greet("Brendan");
## EventsTohavetypesafeevents,youmustuse[TauriSpectav2](./v2.md)!