- How do I install spatie/typescript-transformer in a Laravel project?
- Use Composer to install the package with `composer require spatie/typescript-transformer`. The package requires PHP 8.0+ and integrates seamlessly with Laravel’s autoloader. Follow the official documentation for configuration steps, which typically involve publishing the config file and defining your PHP class paths.
- Does this package support Laravel Eloquent models for TypeScript generation?
- Yes, the transformer can generate TypeScript types from Eloquent models if they are included in your configured scan paths. You’ll need to ensure the model’s properties are properly typed in PHP (e.g., using `protected $fillable` or explicit type hints) for accurate TypeScript output. Custom transformers can further refine the generated types.
- Can I customize how PHP classes are converted to TypeScript?
- Absolutely. The package supports extensible transformers, allowing you to override default behavior for specific classes or namespaces. For example, you can exclude certain properties, rename fields, or map PHP types to custom TypeScript types. Check the documentation for creating and registering custom transformers.
- Will this work in my CI/CD pipeline to keep TypeScript types updated?
- Yes, the transformer is designed for CI/CD integration. You can run it as part of your build script to regenerate TypeScript definitions whenever PHP classes change. The output can be committed to your repository or used to validate frontend-backend type consistency during testing.
- What Laravel versions does spatie/typescript-transformer support?
- The package is built for modern Laravel applications and requires PHP 8.0+. While it isn’t Laravel-specific, it leverages Laravel’s autoloader and namespace conventions. Always check the package’s documentation for the latest Laravel version compatibility, as requirements may evolve with updates.
- How does the transformer handle nested DTOs or complex class hierarchies?
- The transformer recursively processes nested DTOs and class hierarchies, generating TypeScript interfaces that mirror the PHP structure. For example, a DTO containing another DTO will produce a TypeScript interface with a nested type. You can configure depth limits or exclude specific classes if needed.
- Is there a way to test the generated TypeScript before merging it into the frontend?
- Yes, you can validate the generated TypeScript against your frontend codebase using tools like `tsc` (TypeScript compiler) or integration tests. The transformer outputs standard TypeScript files, so they can be linted, compiled, or tested independently. Automate this step in your CI pipeline for reliability.
- What are the performance implications of running this in a large codebase?
- Performance depends on the number of classes scanned and the complexity of the transformations. For large codebases, consider limiting scan paths to only the relevant directories or namespaces. The transformer is optimized for efficiency, but profiling in your CI environment is recommended for production workloads.
- Are there alternatives to spatie/typescript-transformer for Laravel?
- Yes, alternatives include custom scripts using PHP reflection or tools like `php2ts` for basic type conversion. For Laravel-specific solutions, packages like `spatie/laravel-typescript-transformer` (if available) or `nWidart/laravel-models-ts` might offer tighter integration. Evaluate based on features like DTO support, customization, and CI compatibility.
- How do I handle TypeScript enums generated from PHP enums or constants?
- The transformer automatically converts PHP enums and constants into TypeScript enums or union types. For example, a PHP enum `Status::ACTIVE` will generate a TypeScript enum or a union type like `type Status = 'ACTIVE' | 'INACTIVE'`. You can customize this behavior via transformers if your frontend uses a different enum syntax.