InertiaProps structs, ensuring type safety between your backend and frontend.
Generating Types
Run the generate-types command:#[derive(InertiaProps)] structs and generates TypeScript interfaces in frontend/src/types/inertia-props.ts.
How It Works
Given this Rust code:Type Mappings
Kit converts Rust types to TypeScript equivalents:| Rust Type | TypeScript Type |
|---|---|
String, &str | string |
i8, i16, i32, i64 | number |
u8, u16, u32, u64 | number |
f32, f64 | number |
bool | boolean |
Option<T> | T | null |
Vec<T> | T[] |
HashMap<K, V> | Record<K, V> |
| Custom structs | Generated interface |
Using Generated Types
Import types in your React components:Nested Structs
Nested structs are automatically included:Best Practices
Run After Schema Changes
Regenerate types whenever you modify props:Add to Build Process
Include type generation in your development workflow:Use Strict Mode
Enable strict TypeScript checking intsconfig.json:
Handle Optional Values
Always handlenull cases for Option<T> fields:
Workflow Integration
Development Workflow
- Define props in Rust controller
- Run
kit generate-types - Import types in React component
- Get full autocomplete and type checking
Example Workflow
FormRequest Types for Type-Safe Forms
FormRequests can also generate TypeScript types, enabling end-to-end type safety for form submissions.Generating FormRequest Types
Add#[derive(InertiaProps)] to your FormRequest structs:
kit generate-types to generate:
Type-Safe Forms with Inertia
Use the generated type with Inertia’s<Form> component for the cleanest approach:
useForm with the generated types:
Benefits
- Field name autocomplete: TypeScript suggests valid field names
- Type checking: Catch type mismatches at compile time
- Validation alignment: TypeScript types match Rust validation rules
- Error handling: The
errorsobject has matching field keys
For more information on requests and validation, see Requests.
Type-Safe Routes (Inertia v2+)
Kit generates type-safe route helpers that work natively with Inertia.js v2+UrlMethodPair interface. This enables fully type-safe navigation without manually typing URLs or HTTP methods.
Generated Output
Runningkit generate-types also generates frontend/src/types/routes.ts:
Usage with Inertia.js
The generatedcontrollers object works directly with Inertia v2’s native APIs:
Path Parameters
Routes with path parameters like/users/{id} automatically generate typed parameter objects:
How Route Scanning Works
Kit scans yoursrc/routes.rs file and extracts:
- HTTP method:
get,post,put,patch,delete - Path: Including path parameters like
{id} - Handler: Module and function name (e.g.,
controllers::user::show) - Route name: Optional
.name("route.name")suffix
Named Routes Lookup
Named routes are exported as a lookup object for easy access:Benefits
| Benefit | Description |
|---|---|
| Type-safe URLs | No more typos in route paths |
| Type-safe methods | HTTP method is always correct |
| Type-safe params | Path parameters are validated |
| IDE autocomplete | Full IntelliSense support |
| Native Inertia support | Works directly with v2+ APIs |
Troubleshooting
Types Not Updating
If types aren’t reflecting your changes:- Ensure structs have
#[derive(InertiaProps)] - Run
kit generate-typesagain - Restart your TypeScript language server
Missing Nested Types
For nested structs to be included, they must be:- Used in an
InertiaPropsstruct - Have
#[derive(Serialize)]
Type Errors
If you get type mismatches:- Check that Rust and TypeScript types align
- Verify
Option<T>handling (generatesT | null) - Ensure vectors generate arrays (
Vec<T>→T[])
Summary
| Command | Description |
|---|---|
kit generate-types | Generate TypeScript from Rust props and routes |
Generated Files
| File | Contents |
|---|---|
frontend/src/types/inertia-props.ts | TypeScript interfaces from #[derive(InertiaProps)] structs |
frontend/src/types/routes.ts | Type-safe route helpers from src/routes.rs |
Features
| Feature | Benefit |
|---|---|
| Automatic generation | No manual type definitions |
| Type safety | Catch errors at compile time |
| Autocomplete | Full IDE support |
| Nested types | Complex structures supported |
| Type-safe routes | URL and method safety with Inertia v2+ |
| Path parameters | Typed parameter objects for dynamic routes |