spatie/laravel-typescript-transformer
Convert PHP classes, enums, and complex types into TypeScript definitions automatically. This Laravel package handles generics, nullable types, and even TypeScript functions, keeping your frontend and backend types in sync.
.d.ts files. That lets us decide to stop maintaining hand-written interfaces, which is where most contract-drift bugs originate.spatie/laravel-data DTOs and native PHP enums as our API shape becomes much easier to justify when those choices automatically yield typed clients. Conversely, if we have no DTO layer, this package has little to transform.route() calls.Adopt when all of the following hold:
Look elsewhere when:
openapi-typescript or equivalent) is the better contract artifact — it serves external consumers, docs, and non-TS clients.Adoption risk to price in: 400 stars and essentially no public dependents means thin external battle-testing. It is Spatie-maintained with a healthy release cadence through Laravel 13, but v3 was a full rewrite (config moved from a config file into a service provider), and recent patch releases are still fixing route/controller generation edge cases — root-route helpers, install-command provider detection, watcher crashes. Treat route and controller generation as newer than the DTO/enum transformers and validate it in a timeboxed spike before committing broadly.
To executives: We can make the PHP backend the single source of truth for our TypeScript frontend with one MIT-licensed dependency from Spatie, a vendor we already trust. It removes a whole category of "the API changed and the frontend didn't" bugs, and it reduces the manual effort of writing and reviewing types on every API change. Cost is near zero; the main investment is a generation step in CI, which is a few hours of engineering.
To engineering: spatie/laravel-typescript-transformer walks our marked PHP classes, enums, controller actions, and routes and emits generated TypeScript types and a typed route() helper. We annotate with #[TypeScript], run the generation command (or watch mode locally), and get compile-time feedback in the frontend the moment a PHP property or endpoint signature changes. It integrates with laravel-data, handles unions, generics, native TS enums, and nullability conventions. We should adopt it incrementally: start with enums and DTOs, add controller and route generation after a spike, and gate it in CI so a stale diff fails the build. It is types-only — we still need a runtime validation layer in the frontend, so we should pair it with Zod rather than treat it as a replacement.
One-line ask: Approve a one-sprint trial — spike the DTO and enum transformers on one feature area, measure the hand-written types we delete, then decide on broader rollout and the CI gate.
How can I help you explore Laravel packages today?