Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Typescript Transformer

Typescript Transformer Laravel Package

spatie/typescript-transformer

Convert your PHP and Laravel types into TypeScript automatically. spatie/typescript-transformer scans classes, enums, and DTOs, then generates matching TS definitions to keep front-end types in sync with your backend and reduce duplication.

Deep Wiki
Context7

spatie/typescript-transformer helps you automatically generate TypeScript definitions from your PHP/Laravel codebase, keeping frontend types in sync with backend models and DTOs. It scans configured classes, transforms them into TypeScript, and writes output files as part of your build or CI workflow.

Ideal for teams using typed frontends, it reduces manual duplication and prevents type drift by deriving types directly from source-of-truth PHP classes.

  • Generate TypeScript interfaces/types from PHP classes (e.g., DTOs)
  • Configurable discovery via directories/namespaces and output paths
  • Extensible via transformers to customize how types are produced
  • Works well in CI/build pipelines to keep types up to date
  • Designed for seamless Laravel integration
Frequently asked questions about Typescript Transformer
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport