- What does spatie/laravel-typescript-transformer do?
- It converts PHP classes, enums, DTOs, and other complex types into TypeScript definitions for Laravel projects. It is a thin Laravel integration over the framework-agnostic spatie/typescript-transformer core and adds a service provider, publishable config, Artisan commands, and Laravel-specific transformers. You opt in by marking classes with the #[TypeScript] attribute, then run the transformer to produce TypeScript files.
- How do I install and run spatie/laravel-typescript-transformer?
- Install it with composer require spatie/laravel-typescript-transformer, then run php artisan typescript:install to publish the configuration. After marking classes with #[TypeScript], run php artisan typescript:transform to generate the TypeScript output. In a monorepo, wire the transform command into your build or dev workflow so generated types stay current.
- How do I mark a PHP class for TypeScript generation?
- Add the #[TypeScript] attribute to the class you want to expose, such as a DTO, API resource, or value object. The package reads its public properties and types and emits a matching TypeScript type. You can also configure which classes are collected rather than annotating every class.
- Which Laravel and PHP versions does this package support?
- The current v3 line targets modern PHP 8 and Laravel applications, while a maintained 2.x line exists to keep Laravel 12 support. Because the package has had breaking changes between major versions, pin both spatie/laravel-typescript-transformer and the underlying spatie/typescript-transformer exactly. Check the official documentation for the precise PHP and Laravel version matrix for the release you choose.
- How are PHP enums converted to TypeScript?
- Backed string enums become TypeScript string literal unions, so an enum with cases 'typescript' and 'php' generates export type Languages = 'typescript' | 'php'. This gives the frontend a narrow, autocompletable type instead of a loose string. Enum handling is part of the package's core type mapping and can be adjusted through configuration or custom transformers where needed.
- How does it handle nullable, union, generic, and nested types?
- The transformer maps PHP scalars and nullability to TypeScript, for example int becomes number and ?string becomes string | null. It also supports union types, generics, and nested class types, which is why it can model complex DTOs and collections. Generated types describe shape only; they do not carry PHP validation rules, casts, or runtime guarantees across the boundary.
- Can I customize the generated TypeScript types?
- Yes. The package is extensible with custom transformers, and its configuration controls how classes, enums, output names, and file writers are handled. Laravel-specific integrations include Eloquent-aware types and laravel-data DTO support. You can generate a single global declaration file or namespaced files depending on your frontend structure.
- Can it generate TypeScript functions or route helpers?
- Yes, the transformer can generate TypeScript functions from PHP code and can produce typed route or controller-action helpers for frontend calls. Route generation depends on naming and controller conventions and has had edge-case bugs around shared controllers and unknown route names. Treat route output as generated code, review it, and keep the generator and core package versions aligned.
- Should I run typescript:transform in production or at request time?
- No. This is a build-time, pull-based generator, not a runtime bridge, so it should run during development or CI, not inside a web request. Regenerate deterministically and diff the output in CI so an unexpected type change fails the build. Also validate a sample of generated types against real API responses, because compiling types can still misdescribe payloads.
- What are the alternatives and main risks compared with OpenAPI or GraphQL?
- If your API contract is already owned by OpenAPI, GraphQL, or protobuf, a schema-first generator is usually a better fit and adding this package can create two competing sources of truth. The main risk here is silent wrongness: generated types can compile while misdescribing the payload, which creates false confidence. The package is MIT licensed and low-coupling, so forking is an escape hatch, but thin adoption means fewer public edge cases are found for you.