spatie/typescript-transformer
Automatically generate TypeScript definitions from your PHP/Laravel code. spatie/typescript-transformer scans classes and types, then outputs .d.ts files so your frontend stays in sync with backend models, DTOs and enums with minimal manual typing.
ClassTransformer, EnumTransformer, AttributedClassTransformer) and writers (e.g., GlobalNamespaceWriter, ModuleWriter), allowing TPMs to customize behavior without monolithic refactoring. This modularity supports incremental adoption (e.g., starting with enums before full class conversion).LaravelAttributedClassTransformer and Laravel-specific CLI integration (php artisan typescript:transform) reduce friction for Laravel teams, making it a low-effort addition to existing workflows.TypeScriptTransformerConfigFactory) simplifies setup, with options to:
transformDirectories()).DateTime → string).mixed becomes any, which could weaken type safety.@var annotations or custom transformers for edge cases. The package’s replaceType() method allows manual overrides.laravel-mix or vite for hot-reloading.transformDirectories() to limit scope and cache reflection results if extending the package.typescript:transform), reducing setup time.TypeScriptTransformerServiceProvider for runtime integration (e.g., generating types on-demand for API responses).laravel-ide-helper (for PHPStorm) or inertiajs (for frontend-backend type sharing)..d.ts files (for types-only) or .ts files (for types + executable code), compatible with:
#[TypeScript]) to TypeScript interfaces for API responses, reducing boilerplate in controllers.Pilot Phase (Low Risk):
#[TypeScript]
class UserRole {
public const ADMIN = 'admin';
public const USER = 'user';
}
Output:
export type UserRole = 'admin' | 'user';
php artisan typescript:transform in GitHub Actions)..d.ts files to the frontend repo (or use a shared monorepo).Full Adoption (Moderate Risk):
User, Order) with #[TypeScript] and configure transformers.replaceType().Advanced Customization (High Risk/Reward):
JsonResource) to auto-generate TypeScript types for API contracts..d.ts files work with VSCode, WebStorm, and PHPStorm for autocompletion.| Phase | Task | Dependencies | Output |
|---|---|---|---|
| Discovery | Audit PHP classes/enums for TypeScript conversion potential. | None | List of candidate classes. |
| Setup | Install package, configure config/typescript-transformer.php. |
PHP 8.1+, Laravel 9+ | Basic config. |
| Pilot | Transform enums/DTOs, test frontend integration. | CI/CD pipeline | Generated .d.ts files. |
| Validation | Manual review of generated types against frontend needs. | Frontend team feedback | Refined config/replacements. |
| Scaling | Annotate domain classes, extend transformers for complex types. | Custom transformers (if needed) | Expanded type coverage. |
| Automation | Integrate into CI/CD, sync with frontend repos. | GitHub Actions/GitLab CI | Automated type generation. |
^3.0) to avoid major version surprises.SymfonyConsoleLogger) and verbose output flags for troubleshooting.#[TypeScript] or included in `transformHow can I help you explore Laravel packages today?