spatie/laravel-typescript-transformer
Convert PHP classes, enums, and more into TypeScript types automatically. Uses attributes to generate TS from your Laravel code, supports nullable and complex types, generics, and even TypeScript functions—keeping your backend and frontend types in sync.
#[TypeScript], #[MapName]) to generate TypeScript types, ensuring consistency with existing codebases. This reduces friction for teams already using Laravel’s data mapping (e.g., spatie/laravel-data).spatie/typescript-transformer, which provides a flexible core for extending transformations (e.g., custom writers, formatters like Prettier). This allows TPMs to tailor the output to project-specific needs (e.g., custom naming conventions, nested types).spatie/laravel-data: For advanced features (e.g., MapName attributes), but the package gracefully degrades if not installed (e.g., falls back to default naming).httpMethodsPriority, output paths). This is a net positive for maintainability.typescript:watch, typescript:transform, and typescript:install for seamless CLI integration.null vs. TypeScript’s undefined) or pragmatic compatibility?node_modules/@types vs. committed .d.ts files).spatie/laravel-data). Minimal overhead for existing Laravel projects..d.ts files compatible with modern tooling (e.g., TypeScript 5+, Vite, Webpack). Supports Prettier for formatting.#[TypeScript]-annotated classes → TypeScript interfaces.route() helper with autocompletion.#[TypeScript], #[MapName]) and identify gaps.composer require spatie/laravel-typescript-transformer.php artisan vendor:publish --provider="Spatie\LaravelTypeScriptTransformer\TypeScriptTransformerServiceProvider".AppServiceProvider:
TypeScriptTransformer::configure(function (LaravelControllerTransformedProvider $provider) {
$provider->httpMethodsPriority(['get', 'post', 'put']);
});
#[TypeScript].php artisan typescript:watch)..d.ts files to frontend project (e.g., resources/js/types).tsconfig.json:
{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./resources/js/types"]
}
}
php-version in CI to enforce.route('post.create')).fetch/axios calls..d.ts files are auto-generated; avoid manual edits. Use typescript:transform to regenerate.spatie/typescript-transformer for breaking changes (e.g., v3.x → v4.x). Test upgrades in staging.README.md or wiki.php artisan typescript:transform.tsc --noEmit).- name: Generate TypeScript types
run: php artisan typescript:transform
- name: TypeScript lint
run: tsc --noEmit
--verbose flag for Artisan commands to diagnose issues.home vs. frontend dashboard).route() helper consistently.typescript:transform on deploy.TYPESCRIPT_TRANSFORMER.md with:
#[TypeScript], #[MapName]).ignore() in service provider to skip non-critical classes.--only-changed flag (if supported) to regenerate only modified files.#[TypeScript] annotations.vendor/ and node_modules/ from watched paths.deploy.php hook).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Transformer crashes on unsupported type | Broken CI/CD pipeline |
How can I help you explore Laravel packages today?