Skip to content

Route Parameters & Developer Experience ​

Advanced DX features make Laravel TypeGen highly customizable, automated, and type-safe.

Route Parameter Safety ​

Laravel TypeGen can inspect all named routes registered in your application and generate TypeScript route mappings. This makes navigation parameters completely type-safe when using tools like Ziggy.

Run the routes generation command:

bash
php artisan typescript:routes
# Or verify route types in CI:
php artisan typescript:routes --check

By default, this writes to resources/js/types/routes.ts:

typescript
export type RouteName =
  | 'users.index'
  | 'users.show';

export type RouteParams<T extends RouteName> =
  T extends 'users.index' ? {} :
  T extends 'users.show' ? { user: number } :
  never;

Strict Parameter Typing ​

TypeGen automatically inspects route signatures (controllers or closures) and custom route key bindings (e.g. {post:slug} or getRouteKeyName()) to resolve parameter types. If a route parameter binds to an Eloquent model's integer primary key, it becomes number; UUIDs, slugs, and string route keys become string.

Optional Parameters ​

If a route contains an optional parameter (like {comment?}), it will be marked optional (?) in TS:

typescript
T extends 'posts.comments' ? { post: number; comment?: number } :

Ziggy Route Integration ​

When routes.ziggy is enabled in configuration (default), TypeGen generates typed RouteParamsMap and declare module 'ziggy-js' declarations:

typescript
export type RouteParamsMap = {
  'users.index': {};
  'users.show': { user: number };
};

declare module 'ziggy-js' {
  interface RouteList {
    'users.index': [];
    'users.show': [{ name: 'user', required: true }];
  }
}

This provides instant IntelliSense autocompletion and strict parameter checking when using Ziggy's route() helper in your Inertia, React, or Vue components.

Wayfinder Route Integration ​

Enable routes.wayfinder in config/typegen.php (next to ziggy) to emit WayfinderRouteDefinition and WayfinderRoutes types for Laravel Wayfinder workflows:

php
'routes' => [
    'ziggy' => true,
    'wayfinder' => true,
],

CI / CD Safety Gates ​

To prevent stale types from being merged into your repository, TypeGen provides a --check flag:

bash
php artisan typescript:generate --check

Instead of writing to disk, this performs an in-memory generation and precisely compares the output against your existing physical files (supporting both single-file and split-mode). If the generated types differ, the command fails with exit code 1. You can easily add this to your GitHub Actions pipeline to enforce type synchronization!

DB-less CI Builds ​

If your CI pipeline verifies types before database migrations have run, use the --skip-db-check flag (or configure 'strict_schema' => false in config/typegen.php):

bash
php artisan typescript:generate --check --skip-db-check

This prevents RuntimeException: Table does not exist exceptions and falls back to $fillable, $casts, and PHPDoc @property definitions.


Watch Mode ​

During active development, manually running the generation command is annoying. You can start TypeGen in watch mode:

bash
php artisan typescript:generate --watch

TypeGen will run in the background, checking the modified times of your models, enums, form requests, and the typegen.php config file every second. When any changes are detected, it regenerates the types instantly.

Watch mode is pure PHP, cross-platform, and lightweight.


File Splitting ​

By default, all types are emitted to a single file. For large codebases, you might want separate files for each type interface.

Enable splitting in config/typegen.php:

php
'output' => [
    'path' => resource_path('js/types/generated.ts'),
    'split' => true,
    'type_only_imports' => true, // emits `import type { ... }` for verbatimModuleSyntax
],

When split is true:

  1. TypeGen creates a directory called generated at resources/js/types/generated/.
  2. Each type is written to its own file (e.g. User.ts, StorePostRequest.ts).
  3. TypeGen automatically resolves and injects necessary relative imports at the top of each file (e.g. import { Post } from './Post').
  4. A central index.ts barrel file is created which exports all individual types.

Custom Cast Mappings ​

If your model casts database fields to custom Cast classes, you can tell TypeGen how to map them to TypeScript types using the cast_map key in config/typegen.php:

php
'cast_map' => [
    \App\Casts\MoneyCast::class => 'number',
    \App\Casts\CustomObjectCast::class => 'MyCustomObject',
],

This ensures that any model casting to MoneyCast yields a type of number in TypeScript instead of unknown.