spatie/laravel-typescript-transformer
Convert PHP classes, enums, and more into TypeScript types automatically. Uses attributes to generate TS from your Laravel code, supports nullable and complex types, generics, and even TypeScript functions—keeping your backend and frontend types in sync.
laravel-data, enums, and generics, making it ideal for projects using these patterns (e.g., DTOs, API resources, or domain-driven design).Carbon or Collection).Adopt if:
route('user.profile') with parameter validation).laravel-data or Laravel’s native enums and need TypeScript equivalents.Look elsewhere if:
"This package bridges our Laravel backend and TypeScript frontend by auto-generating type definitions for models, routes, and controllers—saving our dev team hundreds of hours annually on manual type maintenance. It’s a low-risk, high-reward investment that improves code quality, reduces bugs, and accelerates frontend development. For example, a User model in PHP becomes a strongly typed User interface in TypeScript, enabling IDE autocompletion and runtime checks. The package is battle-tested (383 stars, MIT license) and maintained by Spatie, a trusted Laravel ecosystem contributor."
ROI:
*"This solves our biggest pain point: keeping TypeScript types in sync with Laravel’s evolving backend. Here’s how we’ll leverage it:
#[TypeScript] and run php artisan typescript:transform to output .d.ts files.route() helper with autocompletion for all Laravel routes (e.g., route('posts.store')).php artisan typescript:watch to auto-regenerate types during development.Why this over custom solutions?
Carbon, Collection, laravel-data).Next steps:
spatie/laravel-typescript-transformer to composer.json.AppServiceProvider.php artisan typescript:install to set up the watcher.#[TypeScript].Example:
// PHP (Laravel)
#[TypeScript]
class CreatePostRequest {
public string $title;
public string|null $content;
}
// Auto-generated TypeScript
export type CreatePostRequest = {
title: string;
content: string | null;
};
```"*
**Key benefits for devs**:
- **No more "any" types**: Frontend gets accurate, up-to-date types.
- **Fewer API docs**: Types serve as self-documenting contracts.
- **CI/CD friendly**: Types are generated as part of the build process.
How can I help you explore Laravel packages today?