- How do I install spatie/typescript-transformer in a Laravel app?
- Run composer require spatie/typescript-transformer in your Laravel project. It is a development-time generator, so install it as a dev dependency if you do not need it at runtime. After installation, configure the output directory and writer, annotate the classes or enums you want to expose with #[TypeScript], and run the transform command or Composer script to emit TypeScript declarations.
- How does the #[TypeScript] attribute convert a PHP class to a TypeScript type?
- Add #[TypeScript] to a PHP class, and the transformer reflects its public properties and PHPDoc types to produce a matching TypeScript type. For example, a User class with id, name, and nullable address becomes export type User = { id: number; name: string; address: string | null }. Only annotated classes are transformed, which lets you control which parts of your PHP model become frontend contracts.
- Can PHP enums be converted to TypeScript union types?
- Yes. Backed PHP enums become TypeScript string or number union types based on their case values. A string enum Languages with TYPESCRIPT and PHP cases becomes export type Languages = 'typescript' | 'php'. This keeps enum changes synchronized between backend and frontend.
- Does spatie/typescript-transformer support nullable properties, generics, and complex types?
- Yes, it handles nullable properties, unions, generics, inherited PHPDoc types, and other complex type declarations. Unsupported or malformed PHPDoc may degrade to unknown rather than failing, so validate generated output and keep PHPDoc accurate. Generics and complex types are a core part of the package's feature set.
- How do I configure where the generated TypeScript files are written?
- Configure the output directory and choose a writer, such as GlobalNamespaceWriter('generated.d.ts'), in the transformer configuration. The writer controls how references and imports are resolved, so its paths must align with your frontend tsconfig aliases. Run the transform after configuration changes to regenerate the declaration file.
- Is spatie/typescript-transformer a runtime dependency or a dev dependency?
- It is a development-time code generator that reads PHP via reflection and emits TypeScript, so it should normally be a require-dev dependency. The generated TypeScript files are what your frontend consumes; the package itself is not needed at runtime. Keep generation in CI or local build scripts rather than in production request handling.
- Which Laravel and PHP versions does spatie/typescript-transformer support?
- The package is installed via Composer and its PHP and Laravel compatibility is defined by the package's composer.json constraints. Check the current requirements on Packagist or the Spatie documentation before adding it to an older Laravel app. Because v3 is a ground-up rewrite, confirm that your Laravel and PHP versions are supported by the v3 release you install.
- How do I prevent generated TypeScript types from drifting in CI?
- Generate TypeScript in CI and fail the build if the committed generated files differ, for example with git diff --exit-code. Treat CI output as authoritative because watch mode uses a different reflection path and can diverge. Also be prepared for minor package updates that reformat output and create large diffs without functional changes.
- Can I generate TypeScript functions with spatie/typescript-transformer?
- Yes, the package can generate TypeScript functions from PHP code in addition to type declarations. This is useful when backend logic or helpers need a typed frontend counterpart. Check the documentation for the specific transformer APIs because function generation is a more advanced feature.
- How does spatie/typescript-transformer compare to OpenAPI or other TypeScript generators?
- Alternatives include OpenAPI-first generators, hand-maintained TypeScript mirrors, and other PHP-to-TypeScript tools. spatie/typescript-transformer is strongest when a Laravel app is the system of record and a single team owns both PHP and TypeScript. It is a weaker fit for public partner APIs or multi-service contracts where a language-neutral specification such as OpenAPI is required. Also note that v3 changes extension primitives, so custom work targets v3 only.