spatie/laravel-typescript-transformer
Convert PHP classes, enums, and more into TypeScript types automatically in Laravel. Mark PHP code with attributes, handle complex types and generics, and generate TS-friendly definitions (and even functions) to keep frontend types in sync.
spatie/typescript-transformer (v3), which supports complex PHP constructs (generics, enums, attributes) and custom transformers. Allows TPMs to extend or override default behavior (e.g., for custom DTOs or Laravel-specific types like Carbon).composer require spatie/laravel-typescript-transformertypescript:transform, typescript:watch) for seamless CI/CD or local development workflows.| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Breaking changes | v3.x requires PHP 8.2+ and Laravel 10+. Legacy projects face migration cost. | Phase adoption: Start with v2.x for older stacks, migrate incrementally. |
| Type accuracy | Complex PHP constructs (e.g., generics, attributes) may need custom transformers. | Test edge cases early; leverage TypeReflectors for custom logic. |
| Build overhead | TypeScript generation adds build steps (e.g., watch mode, CI). | Optimize with exclude patterns in config; cache generated files. |
| Dependency bloat | Pulls in spatie/typescript-transformer (~50KB) and optional formatters (e.g., Prettier). |
Audit dependencies; use composer why spatie/laravel-typescript-transformer to validate. |
v1/user.types.ts)?| Phase | Action | Tools/Commands |
|---|---|---|
| Assessment | Audit PHP classes/enums for TypeScript compatibility. | phpstan or manual review. |
| Setup | Install package and configure service provider. | composer require spatie/laravel-typescript-transformer + php artisan vendor:publish --provider="Spatie\LaravelTypeScriptTransformer\TypeScriptTransformerServiceProvider" |
| Pilot | Transform a single module (e.g., User model + controller). |
php artisan typescript:transform (or watch mode). |
| Enforcement | Add [TypeScript] attribute to critical classes. |
Use #[TypeScript] on models, DTOs, and API responses. |
| CI Integration | Add TypeScript generation to build pipeline. | GitHub Actions: php artisan typescript:transform + prettier --write. |
| Frontend Sync | Import generated types into frontend projects. | import type { User } from './generated/types'; |
User).__get()) may not transform cleanly.HasFactory) need manual mapping (e.g., to string).spatie/typescript-transformer ^3.0 (no conflicts with other Spatie packages).User, Product, etc., to ensure API contracts are type-safe.StoreUserRequest → User).route('user.show', { user: userId })).tsc).types/v1/).php artisan typescript:transform --verbose for detailed logs.storage/framework/views for generated TypeScript (or custom output path).null vs. TypeScript undefined (configure nullToOptional).npx tsup --watch equivalent).User, Order).exclude in config to skip non-critical classes.projectReferences in tsconfig.json to link PHP-generated types.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Transformation errors | Build breaks on invalid PHP. | Use --ignore-errors in CI; log warnings instead of failing. |
| Type drift | Frontend/backend types diverge. | Enforce CI checks (e.g., diff generated types between runs). |
| Custom transformer bugs | Incorrect TypeScript output. | Add unit tests for custom transformers. |
| Watch mode instability | High CPU/memory usage. | Limit watched |
How can I help you explore Laravel packages today?