Type Generation
React Router Framework Mode automatically generates TypeScript types from your route configuration, providing type safety for route parameters, loader data, and more.Overview
The type generation system:- Generates types from your
routes.tsconfiguration - Creates route-specific types for params, loader data, and actions
- Updates automatically during development
- Provides IDE autocomplete for route paths and params
Setup
Type generation is enabled automatically when you use the Vite plugin:Generated Files
Types are generated in.react-router/types/:
Add to .gitignore
Add the generated types directory to.gitignore:
TypeScript Configuration
Update yourtsconfig.json to include generated types:
Route Module Types
Each route module gets generated type annotations:Available Types
Each route module exports these types:Route.LoaderArgs
Arguments passed to the loader function:
Route.ActionArgs
Arguments passed to the action function:
Route.ComponentProps
Props for the default route component:
Route.ErrorBoundaryProps
Props for the ErrorBoundary component:
Route.HydrateFallbackProps
Props for the HydrateFallback component:
Route.MiddlewareArgs
Arguments for middleware functions (with future.v8_middleware flag):
Route.ClientMiddlewareArgs
Arguments for client middleware:
Route Parameters
Parameters are automatically typed based on route paths:Loader Data Types
Loader return values are automatically inferred:Action Data Types
Action return values are also typed:Manual Type Generation
Run type generation manually:Watch Mode
Types are automatically regenerated during development when:- Route files are added or removed
routes.tsconfiguration changesreact-router.config.tschanges
Future Flags Types
Future flags are typed in.react-router/types/react-router-config.d.ts:
Server Build Types
The server build exports are typed in+server.d.ts:
Middleware Types
With middleware enabled:Type Safety Best Practices
1. Use Route Types
Always import and use the generated route types:2. Type Return Values Explicitly
For complex return types, add explicit types:3. Use Type Guards
For error boundaries:Troubleshooting
Types Not Updating
- Check that
.react-router/typesis included intsconfig.json - Restart your TypeScript server in VS Code (
Cmd+Shift+P> “Restart TS Server”) - Run
npx react-router typegenmanually
Build Errors
If types cause build errors:- Delete
.react-router/typesdirectory - Run
npx react-router typegen - Restart your IDE
Missing Types
If route types are missing:- Ensure the route file exists
- Check
routes.tsconfiguration is valid - Look for errors in the console