bash
cargo add specta
cargo add tauri-specta --features javascript,typescript
```rust 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.
pub struct MyCustomReturnType { pub some_field: String, }
pub struct MyCustomArgumentType { pub foo: String, pub bar: i32, } ```
```rust
fn greet3() -> MyCustomReturnType { MyCustomReturnType { some_field: "Hello World".into(), } }
fn greet(name: String) -> String { format!("Hello {name}!") } ```
```rust use specta::collecttypes; use taurispecta::{ts, js};
// this example exports your types on startup when in debug mode or in a unit test. You can do whatever.
fn main() { #[cfg(debugassertions)] ts::export(collecttypes![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();
}
fn exportbindings() { ts::export(collecttypes![greet, greet2, greet3], "../src/bindings.ts").unwrap(); js::export(collect_types![greet, greet2, greet3], "../src/bindings.js").unwrap(); } ```
```ts import * as commands from "./bindings"; // This should point to the file we export from Rust
await commands.greet("Brendan"); ```
Run the example:
bash
pnpm i
cd example/
pnpm tauri dev
Created by oscartbeaumont and Brendonovich.