spatie/laravel-typescript-transformer
Generate TypeScript types from your Laravel/PHP code. Convert classes, DTOs and enums (with attributes) into accurate TS definitions, supporting nullable fields, complex/generic types, and even TypeScript function generation via a simple CLI/workflow.
'get' | 'post' instead of string). Directly addresses flakiness in router.visit() calls and improves IDE autocompletion.HEAD, OPTIONS) from generated types by default, reducing frontend bundle size and unnecessary type checks. Aligns with modern SPAs that rarely use non-standard methods.httpMethodsPriority to curate the API contract—teams can whitelist/blacklist methods (e.g., hide DELETE for SPAs) and control the emitted order. Supports gradual migration of backend endpoints to frontend consumers.Adopt When:
string).HEAD, OPTIONS) from generated types.DELETE for SPAs).string-based route types to stricter unions (e.g., upgrading from method: string to method: 'get' | 'post').Look Elsewhere If:
string methods.Attribute routes) that don’t align with this package’s assumptions.HEAD/OPTIONS methods in your frontend types (customize httpMethodsPriority to include them).*"This update eliminates a class of bugs in our Laravel + TypeScript routing by replacing loose string method types with strict unions like 'get' | 'post'. For teams using Inertia.js or Livewire, this means router.visit() calls are now type-safe by default, reducing flakiness in navigation and form submissions.
Key Impact:
HEAD/OPTIONS from types by default, trimming bundle size without manual effort.DELETE for SPAs) via httpMethodsPriority, controlling the API surface exposed to the frontend.httpMethodsPriority in config/typescript-transformer.php to start benefiting.Metric to Track:
HEAD/OPTIONS types).router.visit() or Inertia navigation.*"The 3.2.0 release transforms type safety for routing while giving us explicit control over the API surface. Here’s the breakdown:
New Features:
'get' | 'post' instead of string, eliminating casts for Inertia/Livewire:
router.visit(MyController.update()); // Type-safe with `method: 'put'`
httpMethodsPriority filters and orders emitted methods. Default excludes HEAD/OPTIONS (auto-registered by Laravel but unused in SPAs):
// config/typescript-transformer.php
'http_methods_priority' => ['get', 'post', 'patch'], // Only these are emitted
DELETE) from the frontend via the config or #[TypeScript(ignore: true)].Trade-offs:
method: string types in frontend code may need updates (but this is a one-time fix for long-term safety).Proposal:
httpMethodsPriority to match your SPA’s needs (e.g., ['get', 'post', 'patch']).string-based route types in favor of the new unions.Migration Path:
# Step 1: Update config
php artisan vendor:publish --tag=typescript-transformer-config
# Step 2: Update frontend types (if using string)
router.visit(MyController.action()); // Now requires explicit method union
Risks Mitigated:
httpMethodsPriority."*"Say goodbye to router.visit(MyController.update()) failing because TypeScript didn’t know update was a 'put' method. 3.2.0 makes it just work:
Before:
router.visit(MyController.update()); // ❌ Error: `method` is `string`, not `'put'`
After:
router.visit(MyController.update()); // ✅ Type-safe! `method` is `'put'`
Bonus: It drops HEAD/OPTIONS from types by default—no more clutter from methods you’ll never use.
How to Use:
// config/typescript-transformer.php
'http_methods_priority' => ['get', 'post', 'patch'], // Only these methods are emitted
#[TypeScript(ignore: true)]
public function destroy() { ... } // Never exposed to frontend
HEAD? Add it to the priority list.DELETE? Keep it or exclude it based on your needs.Pro Tip:
php artisan typescript:transform after updating the config to regenerate types.php artisan typescript:watch for live updates during development.Why This Matters:
How can I help you explore Laravel packages today?