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

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.

View on GitHub
Deep Wiki
Context7

Transform PHP types to TypeScript

Frequently asked questions about Typescript Transformer
How do I install spatie/typescript-transformer in a Laravel project?
Run `composer require spatie/typescript-transformer` to install the package. For Laravel-specific CLI integration, consider pairing it with `spatie/laravel-typescript-transformer` or create a custom Artisan command. The package requires PHP 8.0+ and Laravel 8+ for full compatibility.
Can this package transform Eloquent models into TypeScript types?
Yes, the package supports Eloquent models out of the box. Use the `#[TypeScript]` attribute on your model classes, and the transformer will generate corresponding TypeScript interfaces. For complex relationships, ensure your model properties have proper type hints (e.g., `public function getNameAttribute(): string`).
What happens if my PHP class has dynamic properties without type hints?
Dynamic properties (e.g., `public $dynamicProp`) default to `any` in TypeScript, which may reduce type safety. To mitigate this, explicitly type-hint properties or use the `#[TypeScript(Type: 'string')]` attribute to enforce specific types. Test generated output to ensure alignment with your frontend usage.
How do I configure the transformer to scan only specific directories?
Use the `transformDirectories` configuration in your `typescript-transformer.php` config file to specify paths. For example, set `'transformDirectories' => [app_path('Models'), app_path('DTOs')]` to limit scanning to those directories. This avoids processing unrelated classes and improves performance.
Does this package support PHP 8.1’s union types (e.g., `string|int`)?
Yes, the transformer maps PHP 8.1 union types to TypeScript unions (e.g., `string | number`). However, ensure your TypeScript version supports the equivalent syntax. For complex cases, manually override types using the `#[TypeScript(Type: '...')]` attribute or custom replacements in the config.
How do I integrate generated TypeScript files into my frontend build (e.g., Vite or Webpack)?
Add the generated `.d.ts` files to your TypeScript `paths` in `tsconfig.json` (e.g., `./resources/types`). Configure your build tool (e.g., Vite) to copy these files to the output directory. For example, in Vite, use `copy: { patterns: ['resources/types/*.d.ts', 'dist/types'] }` in `vite.config.ts`.
What’s the best way to handle conflicts if I manually edit TypeScript files?
Avoid manual edits to generated files to prevent merge conflicts. If needed, use the `#[TypeScript(Exclude: true)]` attribute to exclude specific classes or properties. For shared types, consider creating a separate ambient `.d.ts` file and merging it using TypeScript’s `declarationMerge` or `composite` projects.
Can I use this package with Laravel Livewire or Inertia.js for frontend-backend type sync?
Absolutely. The transformer works seamlessly with Livewire and Inertia.js by generating types for your backend models, props, or DTOs. For Livewire, annotate your component classes with `#[TypeScript]`, and for Inertia.js, transform your API response DTOs. This ensures your frontend props/types match the backend contracts.
How do I test if the generated TypeScript matches my PHP types correctly?
Write unit tests using the transformer’s `Transformer` interfaces to mock reflection and validate output. For integration tests, compare generated `.d.ts` files against expected snapshots (e.g., using `pest` or `phpunit`). Test edge cases like nullable types, enums, and nested objects to ensure robustness.
Are there alternatives to spatie/typescript-transformer for Laravel?
Alternatives include `php-typescript` (general-purpose PHP-to-TS) or `rector/rector` with custom rules for type mapping. However, `spatie/typescript-transformer` is Laravel-optimized with built-in support for Eloquent, attributes, and Artisan commands. For simpler needs, consider `vimeo/psalm` with TypeScript plugins, though it lacks Laravel-specific integrations.
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky