Typed Routes
Expo Router automatically generates TypeScript types for all your routes, providing type safety for navigation, parameters, and hrefs.How It Works
When you start your development server, Expo Router analyzes yourapp directory and generates type definitions:
node_modules/.expo/types and included in your project.
Type-Safe Hrefs
All routes are typed, providing autocomplete and type checking:Type-Safe Parameters
Dynamic route parameters are automatically typed:app/user/[id].tsx
Navigating with Parameters
Route Type Definition
Expo Router generates a type for all your routes:typed-routes/types.ts:42-59):
Using Typed Routes
Link Component
Router Navigation
useLocalSearchParams
useSegments
hooks.ts:150-165):
Type Generation
Automatic Generation
Types are generated when:- Development server starts (
npx expo start) - Files in
appdirectory change - New routes are added or removed
Manual Generation
Force type regeneration:CI/CD Type Checking
Generate types for CI/CD:Configuration
TypeScript Config
Ensure yourtsconfig.json includes:
tsconfig.json
IDE Setup
For best experience:- VS Code: Install “TypeScript and JavaScript Language Features”
- Restart TS Server:
Cmd/Ctrl + Shift + P→ “TypeScript: Restart TS Server” - Enable IntelliSense: Settings → “TypeScript › Suggest: Paths” (enabled)
Advanced Types
Generic Route Type
Create helper types for routes:Route Parameters Type
Const Assertions
Useas const for literal types:
Common Patterns
Navigation Helper
Typed Link Component
Route Constants
Type Guards
Troubleshooting
Types Not Generated
Solution: Restart development serverTypeScript Errors
“Type ‘string’ is not assignable to type ‘Href’”IDE Not Showing Autocomplete
- Restart TypeScript server:
Cmd/Ctrl + Shift + P→ “TypeScript: Restart TS Server” - Check TypeScript version: Should be 5.0 or higher
- Verify
tsconfig.jsonincludes.expo/types/**/*.ts - Check Expo CLI version: Update to latest
Build Errors
If types work in development but fail in builds:tsconfig.json
Best Practices
Always Type Parameters
Use Const Assertions
Create Route Constants
Validate Dynamic Routes
Migration Guide
From Untyped to Typed
Step 1: Start dev serverNext Steps
Dynamic Routes
Type dynamic route parameters
Navigation
Type-safe navigation methods
Deep Linking
Type-safe deep links
API Routes
Type-safe API endpoints