Overview
Ziggy includes comprehensive TypeScript type definitions that provide:- Type-safe route generation
- Route name autocompletion
- Parameter validation
- Return type safety
- Optional strict type checking
Basic TypeScript Usage
Ziggy’s types work out of the box without any additional setup:Generating Route Types
To enable route name and parameter autocompletion, generate TypeScript definitions from your Laravel routes:resources/js/ziggy.d.ts (or ziggy-routes.d.ts) with types for all your routes:
Generate Types with Config
Generate both the Ziggy config file and TypeScript types:Generate Only Types
Generate only the TypeScript definitions without the config:Custom Output Path
Global Route Function
If you’re using the@routes Blade directive, declare the global route function to make TypeScript aware of it:
route() globally without imports:
tsconfig.json Setup
If you don’t have theziggy-js NPM package installed, add a path alias to load types from your vendor directory:
Strict Route Name Checking
By default, TypeScript allows passing any string toroute(), even if type definitions are generated. Enable strict checking to only allow known route names:
Route Parameters
Required Parameters
TypeScript knows which parameters are required:Optional Parameters
Multiple Parameters
Route Model Binding
TypeScript understands custom binding keys:Query Parameters
Framework Integration
Vue 3 with TypeScript
route() in templates without errors:
React with TypeScript
Complete Example
Router Class Types
Theroute() function without arguments returns a typed Router instance:
Type Definitions Reference
Ziggy exports the following types:RouteList
Generated byziggy:generate --types. Maps route names to their parameter definitions:
Config
Ziggy’s configuration object structure:TypeConfig
Extend this to configure Ziggy’s type checking:Troubleshooting
Types not showing up
- Ensure you’ve generated types:
php artisan ziggy:generate --types - Check
tsconfig.jsonincludes the generated file - Restart your TypeScript server
”Cannot find module ‘ziggy-js’”
Add a path alias intsconfig.json:
Autocomplete not working
Ensure you’ve generated types and enabled module resolution:“strictRouteNames” not enforcing
Make sure your declaration file:- Uses
export {}at the end - Is included in your TypeScript compilation
- Comes after generating types
Next Steps
Vue
Use Ziggy with Vue and TypeScript
React
Use Ziggy with React and TypeScript
Route Helper
Learn all route() function capabilities
JavaScript Frameworks
Framework integration overview