Skip to main content

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.ts configuration
  • 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/:
Important: Do not edit these files directly. They are regenerated automatically.

Add to .gitignore

Add the generated types directory to .gitignore:

TypeScript Configuration

Update your tsconfig.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.ts configuration changes
  • react-router.config.ts changes

Future Flags Types

Future flags are typed in .react-router/types/react-router-config.d.ts:
This enables conditional types based on enabled features.

Server Build Types

The server build exports are typed in +server.d.ts:

Middleware Types

With middleware enabled:
You get typed middleware functions:

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

  1. Check that .react-router/types is included in tsconfig.json
  2. Restart your TypeScript server in VS Code (Cmd+Shift+P > “Restart TS Server”)
  3. Run npx react-router typegen manually

Build Errors

If types cause build errors:
  1. Delete .react-router/types directory
  2. Run npx react-router typegen
  3. Restart your IDE

Missing Types

If route types are missing:
  1. Ensure the route file exists
  2. Check routes.ts configuration is valid
  3. Look for errors in the console

See Also