Skip to main content

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
The base package includes generic types, but you can generate route-specific types for full autocomplete and validation.

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:
This creates 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:
Now you can use route() globally without imports:

tsconfig.json Setup

If you don’t have the ziggy-js NPM package installed, add a path alias to load types from your vendor directory:
This allows TypeScript to find Ziggy’s types even when you’re importing from the vendor folder:

Strict Route Name Checking

By default, TypeScript allows passing any string to route(), even if type definitions are generated. Enable strict checking to only allow known route names:
With strict checking enabled:
Strict checking requires generated types. Run php artisan ziggy:generate --types first.

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

Now use route() in templates without errors:

React with TypeScript

Complete Example

Router Class Types

The route() function without arguments returns a typed Router instance:

Type Definitions Reference

Ziggy exports the following types:

RouteList

Generated by ziggy: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

  1. Ensure you’ve generated types: php artisan ziggy:generate --types
  2. Check tsconfig.json includes the generated file
  3. Restart your TypeScript server

”Cannot find module ‘ziggy-js’”

Add a path alias in tsconfig.json:

Autocomplete not working

Ensure you’ve generated types and enabled module resolution:

“strictRouteNames” not enforcing

Make sure your declaration file:
  1. Uses export {} at the end
  2. Is included in your TypeScript compilation
  3. 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