Tauri Specta
Completely typesafe Tauri commands
Tauri Specta generates TypeScript or JavaScript bindings for Tauri v2 commands and events. The bindings include the argument and return types from your Rust code.
Install
Run these commands in your Tauri Rust crate, usually src-tauri:
These versions use Specta v2 release candidates. Keep the = pins on specta and tauri-specta. Do not mix examples from older release candidates with this setup.
The derive feature on Tauri Specta enables the Event derive macro. You can leave it out if you only use commands.
Register commands
Add #[specta::specta] below #[tauri::command], then collect the commands in a builder.
Keep your existing Tauri plugins and setup code when adding this to an application. Use builder.invoke_handler() instead of a separate tauri::generate_handler! for these commands. Calling .commands(...) again replaces the previous command list.
The export path is relative to the process's working directory. The path above assumes the app starts from src-tauri. Run a debug build to generate the file before checking the frontend.
Call commands
The generated functions call Tauri's IPC API. They do not bypass Tauri permissions or validate arbitrary frontend values before sending them.
Share custom types
Derive specta::Type on custom command arguments and return values. Derive the Serde traits needed by Tauri too:
Register current_user in collect_commands!. Referenced types are collected automatically. Use .typ::<MyType>() on the builder for a type that is not referenced by a command or event.
Tauri Specta uses Serde-aware input and output phases by default. A type with different serialization and deserialization rules can produce separate aliases. Import the generated types rather than assuming the two shapes are identical.
Handle errors
By default, an Err returned by a command rejects the promise. For Result<T, E>, the function returns Promise<T>. TypeScript does not type a promise's rejection value.
Use a result object when you want to inspect errors as values:
For commands that return Result<T, E>, this mode generates { status: "ok"; data: T } | { status: "error"; error: E }. It rethrows JavaScript Error instances. Framework failures can still reach the IPC boundary, so do not treat every rejection as your application error.
ResultUnknown uses the same result shape but types errors as unknown and does not rethrow. DataError uses { data: T; error: null } | { data: null; error: E }. See ErrorHandlingMode for the full behavior.
Events
Derive Event on each event payload and register it with the same builder:
Add .events(...) to the command builder from the setup example. Do not create a second builder in the application. Call builder.mount_events(app) in Tauri's setup callback before using the typed Rust event methods.
With the tauri_specta::Event trait in scope, emit an event from Rust:
Listen in the frontend:
Channels
Use tauri::ipc::Channel<T> for values streamed from a command. Channels must be direct command arguments or return values, not fields inside structs or event payloads.
Register download with the builder before exporting these bindings.
JavaScript output
Enable Tauri Specta's javascript feature, then replace Typescript::default() with specta_typescript::JSDoc::default() and use a .js output path. The generated JavaScript includes JSDoc type annotations.
Troubleshooting
- Command missing from bindings: check both attributes and the
collect_commands!list, then rerun the exporter. - Command does not run: check that Tauri uses the builder's invoke handler and that the command is allowed by your app's permissions.
- Event does not work: register the event and mount events during setup.
Typeis not implemented: derive it on your payload or enable the relevant Specta feature.- Bindings differ after an update: keep release candidate versions pinned and regenerate before building the frontend.
See the API reference and example applications for more configuration options.
